本文介绍了Android的 - 停止广播接收器被杀害与服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立这实现了许多广播接收机的和基于用户设置一个服务中注册它们的应用程序。该服务绑定到它调用它的一些方法的活动。当广播接收器被称为启动服务(或称的OnStart,如果它已经在运行的服务),并把它传递一个字符串,告诉服务做什么。我的问题是当活动被销毁(后退按钮)的服务也被破坏从而杀死广播接收机。

我知道我可以在清单这将意味着在做检查的时候都来看望,如果用户选择了该选项注册接收器。但是对应用非常关键的接收机之一是android.intent.action.HEADSET_PLUG这只能通过程序注册。

所以我想我的问题是,有没有办法把这个广播接收器活跃时,该服务被销毁?如果不是任何人都可以看到一个解决此问题?

谢谢,罗布

解决方案

如果被封杀你的意思是用户终止,在设置应用程序任务杀手或强制停机您的应用程序,然后杀是适当的动词。但是,你的整个过程被封杀 - 这并不意味着你在这里描述的事件链

如果被封杀你的意思是用户通过后退按钮,退出你的活动,那是因为你选择了绑定到该服务,而不是启动它。如果您希望该服务继续执行以往活动的一生中,你必须使用 startService(),并确保有一些道路,其中,用户可以表明他们不再需要这种服务,所以你知道什么时候叫 stopService()

没有。

启动服务,而不是(或可能除)结合的服务。

I have built an application which implements a number of broadcast receivers and registers them within a service based on user settings. The service is bound to an activity which calls some of its methods.When a broadcast receiver is called it starts the service (or calls onstart of the service if it is already running) and passes it a string telling the service what to do. My problem is when the activity is destroyed (back button) the service is also destroyed which in turn kills the broadcast receivers.

I know I can register the receivers in the manifest which would mean doing a check when they are called to see if the user has selected that option. However one of the receivers critical to the application is 'android.intent.action.HEADSET_PLUG' which can only be registered programatically.

So I guess my question is, is there a way to keep this broadcast receiver active when the service is destroyed?If not can anyone see a workaround for this issue?

Thanks,Rob

解决方案

If by "killed" you mean the user terminated your app with a task killer or "Force Stop" in the Settings app, then "killed" is the appropriate verb. However, your whole process is "killed" -- it does not follow the chain of events that you describe here.

If by "killed" you mean the user exited your activity via the BACK button, that is because you elected to bind to the service, rather than start it. If you want the service to continue executing past the lifetime of the activity, you must use startService(), and ensure that there is some path by which the user can indicate that they no longer want this service, so you know when to call stopService().

No.

Start your service, instead of (or possibly in addition to) binding to the service.

这篇关于Android的 - 停止广播接收器被杀害与服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 01:48