如何防止Android服务被杀死(带有通知的服务) [英] How to prevent android service getting killed (Service with notification)

查看:83
本文介绍了如何防止Android服务被杀死(带有通知的服务)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个需要不断显示通知的应用程序.
我有一项服务可以每秒钟显示一次通知.
当资源不足时,android会杀死我的服务.
我已经从onStartCommand()获得了返回值START_STICKY.
因此,一段时间后,服务将重新启动.
但与此同时,通知外观已停止.

I am working on an application which need to show notification constantly.
I've a service to show the notification every seconds.
When the resources goes low android is killing my service.
I've given the return START_STICKY from onStartCommand().
So after some time the service is getting restarted.
But in the mean time the notification look stoped.

我在某个地方变红了,该服务可能显示阻止Android阻止通知的通知.我没有得到任何例子.

I red somewhere that it is possible for a service showing notification prevented from getting stoped by android. I dint get any example for it.

推荐答案

如上所述,您需要使用 startForeground(int,Notification)方法来使用户了解您的服务.它也因此获得更多特权.

As written above you need to use startForeground(int,Notification) method to aware user about your service. It also get more privilage for it.

您可以在此处(在其中的示例)中了解更多信息.

You can read more about it here (example inside).

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

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