我在我的应用程序中使用 Service 并且它需要运行直到我的应用程序被卸载,但问题是它被操作系统杀死了.
I am using Service in my application and it needs to run until my application is uninstalled, but the problem is it gets killed by OS.
我们如何防止它被操作系统杀死?或者如果它被杀死了,我们可以通过编程方式重新启动该服务吗?
How can we prevent it from being killed by OS? Or if it gets killed can we restart that service again through programmatically?
推荐答案您可以在 前台 使用 startForeground().
前台服务是一种被认为是某种东西的服务用户主动知道,因此不是系统的候选人内存不足时杀死.
A foreground service is a service that's considered to be something the user is actively aware of and thus not a candidate for the system to kill when low on memory.
但请记住,前台服务必须为状态栏提供通知(阅读此处),并且除非服务停止或从前台移除,否则无法取消通知.
But bear in mind that a foreground service must provide a notification for the status bar (read here), and that the notification cannot be dismissed unless the service is either stopped or removed from the foreground.
注意:这仍然不能绝对保证在内存极低的情况下服务不会被杀死.这只会使它不太可能被杀死.
Note: This still does not absolutely guarantee that the service won't be killed under extremely low memory conditions. It only makes it less likely to be killed.
更多推荐
我们如何防止服务被操作系统杀死?
发布评论