Android: How to safely unbind a service

后端 未结 8 932
深忆病人
深忆病人 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 17:06

    Not sure about all the above answers, it seemed far too complicated while none of these would fit the issue I had.

    Only binding/unbinding once at a time, and the service was definitely bound at the time of the unbind() call. Don't want to leak anything, so I just made sure I was using the same context for the bind() and unbind() calls and that solved it permanently! Doing something like this:

    any_context.getApplicationContext().bind(...);
    
    ...
    
    another_context.getApplicationContext().unbind(...);
    

提交回复
热议问题