Reputation: 10767
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?
Upvotes: 3
Views: 3483
Reputation: 843
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 :)
Upvotes: 1
Reputation: 10767
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 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);
}
}
then, i created implementation for 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);
Upvotes: 2