在服务停止时调用 onStart()
Calling onStart() on a service while being stopped
我有一个应用程序,我在其中启动了一项服务:
Intent networkingService=new Intent(MainActivity.this, SocketMngr.class);
networkingService.putExtra("FbID",myFbId);
if (mService==null) mService=new Messenger(messageHandler); //mService is private messenger variable of the activity
networkingService.putExtra("MESSENGER", mService);
networkingService.putExtra("REGIDID", regid);
startService(networkingService);
bindService(networkingService, mConnection, Context.BIND_AUTO_CREATE); //mConnection is a private variable of the activity
现在,当用户退出应用程序时,我在我的主 activity 中将一个静态标志变量设置为一个值,该服务将每隔 interval 秒查看一次,以确定是否自杀。
主要activity:
@Override
public void onDestroy()
{
this.SERVICE_KILL_YOURSELF=true; //this flag is false when app launched
}
在服务中,类似于:
while(MainActivity.SERVICE_KILL_YOURSELF==false)
{
ProcessData(); //can take long time thus can be processed while main activity finished
}
stopMySelf()
现在,我正在使用描述的场景,因为当用户退出应用程序时,该服务仍然可以从服务器接收最后的数据包或发送此类数据,只有在那之后,该服务才会结束自己。
现在,如果应用程序销毁后服务仍在处理最后的数据,那么应用程序会在服务查看 MainActivity.SERVICE_KILL_YOURSELF
之前再次启动,然后应用程序在启动时设置 SERVICE_KILL_YOURSELF=false
,然后 onStart()
会被调用,服务不会停止,没问题。
我担心和质疑的是,如果服务查看 MainActivity.SERVICE_KILL_YOURSELF
会发生什么,因为应用程序已被销毁 true
,然后服务开始自行停止,同时它正在停止应用程序再次启动并调用服务的 onStart()
。
在那种情况下会发生什么,有什么我应该担心的吗?
程序退出时可能不会调用onDestroy()
甚至onStop()
你只知道当程序退出时它肯定会调用onPause()
所以使用onDestroy()
时要小心
即使用户关闭了您的应用程序,您的服务也可能保持原样。
我有一个应用程序,我在其中启动了一项服务:
Intent networkingService=new Intent(MainActivity.this, SocketMngr.class);
networkingService.putExtra("FbID",myFbId);
if (mService==null) mService=new Messenger(messageHandler); //mService is private messenger variable of the activity
networkingService.putExtra("MESSENGER", mService);
networkingService.putExtra("REGIDID", regid);
startService(networkingService);
bindService(networkingService, mConnection, Context.BIND_AUTO_CREATE); //mConnection is a private variable of the activity
现在,当用户退出应用程序时,我在我的主 activity 中将一个静态标志变量设置为一个值,该服务将每隔 interval 秒查看一次,以确定是否自杀。
主要activity:
@Override
public void onDestroy()
{
this.SERVICE_KILL_YOURSELF=true; //this flag is false when app launched
}
在服务中,类似于:
while(MainActivity.SERVICE_KILL_YOURSELF==false)
{
ProcessData(); //can take long time thus can be processed while main activity finished
}
stopMySelf()
现在,我正在使用描述的场景,因为当用户退出应用程序时,该服务仍然可以从服务器接收最后的数据包或发送此类数据,只有在那之后,该服务才会结束自己。
现在,如果应用程序销毁后服务仍在处理最后的数据,那么应用程序会在服务查看 MainActivity.SERVICE_KILL_YOURSELF
之前再次启动,然后应用程序在启动时设置 SERVICE_KILL_YOURSELF=false
,然后 onStart()
会被调用,服务不会停止,没问题。
我担心和质疑的是,如果服务查看 MainActivity.SERVICE_KILL_YOURSELF
会发生什么,因为应用程序已被销毁 true
,然后服务开始自行停止,同时它正在停止应用程序再次启动并调用服务的 onStart()
。
在那种情况下会发生什么,有什么我应该担心的吗?
程序退出时可能不会调用onDestroy()
甚至onStop()
你只知道当程序退出时它肯定会调用onPause()
所以使用onDestroy()
时要小心
即使用户关闭了您的应用程序,您的服务也可能保持原样。