Android的:如何绑定服务可以注意到有关被杀客户端? [英] Android: How bound Service can be noticed about killed client?

查看:443
本文介绍了Android的:如何绑定服务可以注意到有关被杀客户端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动和服务绑定到它。

I have an Activity and Service bound to it.

如果服务由系统被杀,活动是因为 ServiceConnection.onServiceDisconnected(组件名)注意到它会调用。

If Service is killed by system, Activity is noticed about it because of ServiceConnection.onServiceDisconnected(ComponentName) will be called.

但是,当活动的过程是由系统杀死,没有任何回调的服务?怎样才能服务知道客户端被打死? (当整个进程被终止,没有的onDestroy()方法调用,可惜)

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:

我在服务回调列表:

RemoteCallbackList<IModConnClient> mCallbacks

当我开始通过调用广播 mCallbacks.beginBroadcast(); 返回我的一些客户。所以,我能记住客户的previous号,并检查它是否下降。但它似乎并没有被真正好的解决方案。我想有一定的回调,但不幸的是我无法找到一个。

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.

谁能给一个建议?

推荐答案

我找到了解决办法。

正如 RemoteCallbackList 文档,如果注册的回调的过程中消失了,这个类会照顾从列表中自动删除它。如果你想要做更多的工作,在这种情况下,你可以创建一个实现onCallbackDied(E)方法的子类。

所以,我创建了一个子类 RemoteCallbackList 接口 IRemoteCallbackDied

So, i created a subclass for RemoteCallbackList and interface IRemoteCallbackDied:

   private interface IRemoteCallbackDied<E extends IInterface> {
      public void remoteCallbackDied(E callback, Object cookie);
   }

   private final class RemoteCallbackListWithDiedCB<E extends IInterface> extends RemoteCallbackList<E> {
      IRemoteCallbackDied<E> mRemoteCallbackDied;

      public RemoteCallbackListWithDiedCB(IRemoteCallbackDied<E> mRemoteCallbackDied)
      {
         this.mRemoteCallbackDied = mRemoteCallbackDied;
      }

      @Override public void onCallbackDied(E callback, Object cookie){
         super.onCallbackDied(callback, cookie);
         this.mRemoteCallbackDied.remoteCallbackDied(callback, cookie);
      }
   }

然后,我创建实施 IRemoteCallabackDied

   private final IRemoteCallbackDied<IModConnClient> mRemoteCallbackDied = new IRemoteCallbackDied<IModConnClient>() {
      public void remoteCallbackDied(IModConnClient mModConnClient, Object cookie)
      {
         Log.v("my_tag", "remote callback died!");
         /*
          * Some code
          */
      }
   };

和,终于,我的回调列表的定义就是这样:

and, finally, my callback list is defined just like that:

private RemoteCallbackListWithDiedCB<IModConnClient> mClients = new RemoteCallbackListWithDiedCB<IModConnClient>(mRemoteCallbackDied);

这篇关于Android的:如何绑定服务可以注意到有关被杀客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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