简体   繁体   English

Android-管理跨多个活动与本地服务绑定/解除绑定的有效方法

[英]Android - effective way to manage binding/unbinding from local service across multiple activities

Targeting and building from API Level 4 and above. 从API级别4及更高版本进行定位和构建。

Right now, I'm dealing with an issue in which I'm trying to maintain bindings to my local service across multiple activities, and stop the service when the last connection is unbound. 目前,我正在处理一个问题,其中我试图跨多个活动维护与本地服务的绑定,并在最后一个连接解除绑定时停止该服务。

In a nutshell, my service just calls a system service in a HandlerThread that returns quickly to a BroadcastReceiver, then does that same call again after waiting a pre-determined amount of time (at least 15 seconds). 简而言之,我的服务只是在HandlerThread中调用系统服务,该服务迅速返回到BroadcastReceiver,然后在等待预定时间(至少15秒)后再次执行相同的调用。

Suppose I have my base activity create the first bond to my service in onCreate() in this fashion: 假设我有基本活动以这种方式在onCreate()中创建服务的第一个绑定:

Intent service = new Intent(ActivityA.this, MyLocalService.class);
getApplicationContext().bindService(service, mConnection, BIND_AUTO_CREATE);

Suppose also that due to the fact that I maintain my binding across screen rotations by carrying over the binder and connection, I do not unbind from the service until the activity is finished: //onRetainNonConfigurationInstance carries over my binder and connection since i bound from the app context, so they are fair game. 还假设由于我通过继承活页夹和连接来保持屏幕旋转之间的绑定,因此在活动完成之前我不会与服务解除绑定:// onRetainNonConfigurationInstance继承我的活页夹和连接,因为我从应用上下文,因此它们是公平的游戏。

public void onDestroy(){
   super.onDestroy();
   //using binder, remove callback to service from current activity
   if(isFinishing(){
      getApplicationContext().unbindService(mConnection);
   }
}

I pretty much do this setup for any other Activity that wants to listen in on the service. 我几乎要为要监听该服务的其他任何活动进行此设置。

My problem is that eventually, some activities don't unbind instantly, hence the service will still hang around as per the behavior of the bind/unbind pattern if the service is auto created. 我的问题是,最终,某些活动不会立即解除绑定,因此如果服务是自动创建的,则该服务仍会按照绑定/解除绑定模式的行为徘徊。 I had to go as far as stopping my thread before unbinding on the last activity, which prevented any system services being called in the BG. 在取消对最后一个活动的绑定之前,我不得不停止线程,这阻止了BG中任何系统服务的调用。 Is there a better way to manage binding and unbinding services, or am I doing my best with my current setup? 有没有更好的方法来管理绑定和取消绑定服务,或者我在当前设置下会尽力吗? Also, since my service (via the binder) is weakly referenced, would this reduce my risk of leaking memory? 另外,由于我的服务(通过活页夹)被弱引用,这是否会减少我泄漏内存的风险?

Apparently, it wasn't un-registering due to the fact that I was not passing the binding across rotations and that I was using an internal state flag to determine whether or not the activity was rotating (was set to true when onRetainNonConfigurationInstance() was called). 显然,它并未取消注册,原因是我没有跨旋转传递绑定,并且我正在使用内部状态标志来确定活动是否在旋转(当onRetainNonConfigurationInstance()被设置为true时)称为)。 In most cases, it was not reading the state properly in onDestroy(). 在大多数情况下,它无法在onDestroy()中正确读取状态。

I ended up unbinding when isFinishing() resolved to true in onDestroy() as well as passing the bindings through onRetainNonConfigurationInstance(), and as a result, the service was able to shut down on the last unbinding. 当isFinishing()在onDestroy()中解析为true并通过onRetainNonConfigurationInstance()传递绑定时,我最终解除了绑定,结果,该服务能够在最后一次解除绑定时关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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