Android服务不会自行重启
在我的应用程序中,我有一个活动和一个服务(extends IntentService)。该服务的onStart命令如下所示Android服务不会自行重启,android,Android,在我的应用程序中,我有一个活动和一个服务(extends IntentService)。该服务的onStart命令如下所示 @Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); return START_REDELIVER_INTENT; } 我的onHandleIntent方法: @Ov
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_REDELIVER_INTENT;
}
我的onHandleIntent方法:
@Override
protected void onHandleIntent(Intent intent) {
while(continueLoop){ //continueLoop is controlled by the Binder
//Do stuff
}
}
我还从活动绑定到服务,因此我可以打破无限循环。我启动了应用程序及其服务,然后启动了其他应用程序,一段时间后,我的活动停止并被破坏,我的服务也停止了。当我使用任务管理器关闭其他应用程序时,服务不会自行启动 我等了一会儿,然后启动了我的应用程序,活动一启动,服务也就启动了。我认为android系统会在内存可用时自动重启服务。我是错过了什么,还是应该再等一段时间 请根据文档提前感谢: 客户端通过
startService(Intent)
调用发送请求;该服务根据需要启动,使用辅助线程依次处理每个Intent
,并在工作结束时自行停止
如果您自己绑定到服务和/或控制服务的生命周期,那么您应该使用服务
,而不是意向服务
,如果您阅读本文,您会看到
onStartCommand(意向、int标志、int startId)您不应该为您的IntentService重写此方法 反而 IntentService类的存在是为了简化此模式并处理机制要使用它,请扩展IntentService并实现OnHandleContent(Intent)。
“我也从活动绑定到服务”--但是您是否调用过
startActivity()
?您是指startService吗?我调用它,然后绑定到服务来控制它。