Android: How to safely unbind a service

后端 未结 8 929
深忆病人
深忆病人 2020-12-12 16:44

I have a service which is binded to application context like this:

getApplicationContext().bindService(
                    new Intent(this, ServiceUI.class         


        
8条回答
  •  春和景丽
    2020-12-12 16:52

    Try this:

    boolean isBound = false;
    ...
    isBound = getApplicationContext().bindService( new Intent(getApplicationContext(), ServiceUI.class), serviceConnection, Context.BIND_AUTO_CREATE );
    ...
    if (isBound)
        getApplicationContext().unbindService(serviceConnection);
    

    Note:

    You should use same context for binding a service and unbinding a service. If you are binding Service with getApplicationContext() so you should also use getApplicationContext.unbindService(..)

提交回复
热议问题