简体   繁体   中英

Android: How to tell when a bound service has been destroyed?

I've got a service that's bound to from a couple of activities, each using a ServiceConnection .

Each activity needs to check before calling the service whether the service is already in use. So in the service I have a function (let's say getCurrentId() ) which returns details of what the service is currently doing. Then in the client activity, the service connection is set up:

private MyService mService = null;
private ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder binder) {
        MyService.MyBinder myBinder = (MyService.MyBinder) binder;
        mService = myBinder.getService();
        activeId = mService.getCurrentId();
        log.i(TAG, "Service bound" );
    }
    public void onServiceDisconnected(ComponentName className) {
        log.i(TAG, "Service has been killed");
        mService = null;
    }
};  

A button toggles binding to the service:

activity.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

and unbinding:

activity.unbindService(mConnection);

I'm not calling startService() at all.

Before I bind to the service, I check if it's already active and what it's doing:

if (mService == null)
    activeId = -1;
else
    activeId = mService.getCurrentId();

The problem is , if an activity binds to and then unbinds from the service, the service onDestroy() method is called (I've logging in it to confirm this), which is fine. BUT this doesn't trigger onServiceDisconnected() .

So mService is never set to null, and when I get to that if statement, it happily carries on and calls getCurrentId() , which returns whatever the previous details were.

I gather that onServiceDisconnected() is only supposed to be called when the thread the service is running in is unexpectedly killed, so it's correct that it's not called when the service is destroyed due to the last activity using it unbinding.

As far as I can tell, the service isn't being reinstantiated, I've got logging throughout it.

Which gives me two questions:

Is there an alternative callback function or some way where a ServiceConnection is notified that its service has been destroyed by unbinding?

If the service has been destroyed, then how can I still call its functions? Or is something else going on - is the ServiceConnection or the Binder somehow returning the value without actually calling the service?

onServiceDisconnected() is only called

when a connection to the Service has been lost. This typically happens when the process hosting the service has crashed or been killed.

Quoted from the Android docs . This seems to be a very rare case, and will not be called when simply unbinding normally from a service.

To keep my connections with a service sane, I would suggest you bind to the service in the Activities onResume method and unbind from it in the onPause method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM