好的,因为这已被证明是一个特别困难的问题,我想我会在这里发布我的解决方案,以防有人遇到类似的问题。
显然有几种方法可以解决这个问题,但我使用过的最简单的方法是拥有一个“启动”服务来决定何时自行关闭。我的活动每次都绑定/取消绑定到服务,并且在设定的时间延迟(我用了 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()
触发计时器。