我们如何防止服务被操作系统杀死?

编程入门 行业动态 更新时间:2024-10-28 12:27:37
本文介绍了我们如何防止服务被操作系统杀死?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

更多推荐

我们如何防止服务被操作系统杀死?

本文发布于:2023-11-27 05:52:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636854.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何防止   被操   系统

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!