好吧,由于这已被证明是一个特别困难的问题,我想我会在这里发布我的解决方案,以防有人遇到类似的东西。
显然有几种方法可以解决这个问题,但我用过的最简单的方法是有一个“启动”服务来决定何时关闭自己。我的活动每个绑定/取消绑定到服务,并且在设置的时间延迟(我使用了1分钟)后,如果没有进一步的活动绑定,服务将关闭 - 这涵盖了用户是否停止使用应用程序以及是否存在任何致命的活动错误。
关机计时器在 内安排,并在 、 、 中取消。如果它触发,它将完全关闭服务,这反过来又会触发对服务的 托管状态的清理。onUnbind()
onStartCommand()
onBind()
onRebind()
onDestroy()
我的代码如下:Service
public class LocalStateService extends Service {
/** The binder to give to clients. */
private final IBinder binder = new LocalStateBinder();
/** Used for time-delayed shutdown. */
private final Handler handler = new Handler();
/**
* Called before starting or the first binding.
*/
@Override
public void onCreate() {
// initialise state...
}
/**
* Called when this service is explicitly started.
* @param intent The intent passed on starting, unused
* @param flags Startup flags, unused
* @param startId Identifies each start request
* @return Desired restart behaviour
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
cancelShutdown();
// if killed, we would like Android to restart this service, but don't bother re-delivering
// the original intent used to start the service
return START_STICKY;
}
/**
* Called when the first client binds.
* @param intent The intent passed on binding
* @return The binding to use
*/
@Override
public IBinder onBind(Intent intent) {
cancelShutdown();
return binder;
}
/**
* Called when the first of previous clients re-binds.
* @param intent The intent passed on binding
*/
@Override
public void onRebind(Intent intent) {
cancelShutdown();
}
/**
* Called when all clients have unbound.
* @param intent The first intent originally passed on binding
* @return Whether this service should be notified of rebinding
*/
@Override
public boolean onUnbind(Intent intent) {
// post a callback to be run in 1 minute
handler.postDelayed(delayedShutdown, 1000L * 60);
// we do want onRebind called when clients return
return true;
}
@Override
public void onDestroy() {
// state cleanup...
}
private Runnable delayedShutdown = new Runnable() {
@Override
public void run() {
LocalStateService.this.stopSelf();
}
};
/**
* Cancel any shutdown timer that may have been set.
*/
private void cancelShutdown() {
// remove any shutdown callbacks registered
handler.removeCallbacks(delayedShutdown);
}
}
我的主要活动不是从我的 开始这样做,因为这既适用于初始启动,也适用于用户返回使用暂停的应用程序(服务可能决定关闭,也可能不决定自行关闭)。Application
startService(..)
onCreate()
然后,每个活动都像往常一样绑定和解绑。
我发现:
在活动之间导航时,不会触发任何服务回调。由于活动生命周期重叠,这些是辅助绑定/取消绑定
当活动重新启动(例如屏幕旋转)时,服务将收到一个呼叫onUnbind()
onRebind()
暂停应用(例如,从主要活动按回主活动)或完成时(例如,从主要活动按回),服务将获取计时器。onUnbind()