Android服务onBind方法接收过时的意图
我正在实例化一个服务,如下所示:Android服务onBind方法接收过时的意图,android,android-intent,android-service,Android,Android Intent,Android Service,我正在实例化一个服务,如下所示: try { Intent intent = new Intent(this, SOMEService.class); intent.putExtra("key", SOME UUID); startService(intent); bindService(intent, myServiceConnection, Context.BIND_AUTO_CREATE); } catch (Exce
try {
Intent intent = new Intent(this, SOMEService.class);
intent.putExtra("key", SOME UUID);
startService(intent);
bindService(intent, myServiceConnection, Context.BIND_AUTO_CREATE);
} catch (Exception e) {
}
在服务的onBind方法中,我检查提供的密钥是否与服务中的预期密钥匹配。
每次事务都会刷新此密钥
问题是,如果我关闭应用程序并重新打开它(强制服务重新绑定),在这之后,onBind方法使用陈旧密钥接收到以前的意图,而不是使用新密钥接收到新的意图
知道为什么会发生这种情况吗?当您启动该服务时,它将不间断地运行并重新启动,即使Android因资源不足而停止它。所以,当你们关闭应用程序时,服务会一直运行——你们只需要从中绑定。如果您希望您的服务仅在应用程序打开时运行,请在应用程序关闭时停止,然后重新启动。您是否尝试在onStartCommand?中检查意图,因为您也在启动服务 根据文件: 每次客户端显式启动服务时由系统调用 通过调用startService(意图) 例如:
public void onStartCommand (Intent intent,
int flags,
int startId)
{
String key = intent.getStringExtra("key");
}
到目前为止,我已经覆盖了我的服务的onStartCommand方法,以返回START\u NOT\u STICKY标志。这解决了问题。虽然从长远来看,我应该想办法正确地停止我的服务。