我们如何防止服务被操作系统杀死? [英] How can we prevent a Service from being killed by OS?

查看:25
本文介绍了我们如何防止服务被操作系统杀死?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用 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.

这篇关于我们如何防止服务被操作系统杀死?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆