Android:如何绑定服务才能注意到被杀死的客户端?
我有一个绑定到它的活动和服务。
如果 Service 被系统杀死,Activity 会注意到它,因为 ServiceConnection.onServiceDisconnected(ComponentName) 将被调用。
但是当Activity的进程被系统杀死后,Service有回调吗?服务如何知道客户端已被杀死? (不幸的是,当整个进程被终止时,没有 onDestroy()
方法调用)
我现在看到的唯一方法如下:
我的 Service
中有回调列表:
RemoteCallbackList<IModConnClient> mCallbacks
当我通过调用 mCallbacks.beginBroadcast(); 开始广播时,它会返回一些客户端。所以,我可以记住以前的客户数量并检查它是否减少了。但这似乎并不是一个很好的解决方案。我想要一些回电,但不幸的是我找不到。
有人可以提出建议吗?
I have an Activity and Service bound to it.
If Service is killed by system, Activity is noticed about it because of ServiceConnection.onServiceDisconnected(ComponentName) will be called.
But when Activity's process is killed by the system, is there any callback to Service? How can Service know that client was killed? (When the whole process is killed, there's no onDestroy()
method call, unfortunately)
The only way i see now is the following:
I have callback list in my Service
:
RemoteCallbackList<IModConnClient> mCallbacks
When i begin broadcast by calling mCallbacks.beginBroadcast();
it returns me a number of clients. So, i can remember previous number of clients and check if it decreased. But it seems not to be really good solution. I'd like to have some callback, but unfortunately i can't find one.
Can anyone give a suggestion?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了解决方案。
如
RemoteCallbackList
文档中所述,“如果注册的回调进程消失,这个类会自动将其从列表中删除。如果您想在这种情况下执行额外的工作,您可以创建一个实现 onCallbackDied(E) 方法的子类。”因此,我创建了一个子类
RemoteCallbackList
和接口IRemoteCallbackDied
:然后,我创建了
IRemoteCallabackDied
的实现:最后,我的回调列表的定义如下:
I found the solution.
As stated in
RemoteCallbackList
documentation, "If a registered callback's process goes away, this class will take care of automatically removing it from the list. If you want to do additional work in this situation, you can create a subclass that implements the onCallbackDied(E) method."So, i created a subclass for
RemoteCallbackList
and interfaceIRemoteCallbackDied
:then, i created implementation for
IRemoteCallabackDied
:and, finally, my callback list is defined just like that:
还有一个简单的解决方案。它是onTaskRemoved 服务方法。当活动被终止时它会让您知道。在文档上。它说......
public void onTaskRemoved (Intent rootIntent)
添加于 API 级别 14
如果服务当前正在运行并且用户已删除来自服务应用程序的任务,则会调用此函数。如果你设置了ServiceInfo.FLAG_STOP_WITH_TASK那么你将不会收到这个回调;相反,该服务将被简单地停止。
参数
rootIntent 用于启动要删除的任务的原始根 Intent。
如果你想知道整个进程何时在没有 onDestroy 的情况下被杀死,你需要了解 死亡接收者。有一篇友善的博客文章。
我希望这个答案能以另一种方式帮助你:)
There is a simple solution too. It is onTaskRemoved method of Service. It will let you know when an activity got killed. On the doc. it says..........
public void onTaskRemoved (Intent rootIntent)
Added in API level 14
This is called if the service is currently running and the user has removed a task that comes from the service's application. If you have set ServiceInfo.FLAG_STOP_WITH_TASK then you will not receive this callback; instead, the service will simply be stopped.
Parameters
rootIntent The original root Intent that was used to launch the task that is being removed.
If you want to know when the whole process is killed without onDestroy, you need to know about DeathRecipient. There is a kind blog post.
I hope this answer would help you in anther way :)