[英]Android Service (In its own thread) dies when app is killed then restarts
I've been struggling with this question for weeks now. 我已经为这个问题苦苦挣扎了好几个星期了。 I'm fairly new to Android, hopefully you can give me a hand. 我刚接触Android,希望您能帮上忙。
I have this service which runs on a separate thread than the app's. 我有这项服务,它运行在与应用程序不同的线程上。 Essentially, the user instructs it to start, and it should stay alive either until the user tells it to stop or until it has served its purpose - it schedules its own destruction (stop) when needed. 从本质上讲,用户指示它启动,并且它应该一直处于活动状态,直到用户指示它停止运行或达到其目的为止-它会在需要时安排自己的销毁(停止)时间。 The service needs to stay alive as it holds important priority-related information, so I can't simply turn to the alarm manager to revive it when needed - though I do use alarm manager for other purposes. 该服务需要保持活跃状态,因为它包含与优先级相关的重要信息,因此尽管需要将警报管理器用于其他目的,但我不能简单地求助于警报管理器来恢复它。 I'm having two problems: 我有两个问题:
Secondly, it restarts itself, thus causing the data to be re-loaded hence, I lose the data. 其次,它会自行重启,从而导致数据被重新加载,因此,我丢失了数据。
As for the activity, its binding to the server through: 至于活动,它通过以下方式绑定到服务器:
private void startService() {
startService(new Intent(this, CES.class));
bindService(new Intent(this, CES.class), mConnection, 0);
}
Finally, the relevant (or at least the ones I find relevant) methods in the Service: 最后,服务中的相关方法(或至少我发现相关的方法):
private final IBinder mBinder = new ICESInterface.Stub() { ..... }
@Override
public void onCreate() {
//keeps being called thus I lose my data }
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent,flags,startId);
//return START_STICKY;
}
Let me know if there is more data/information you need. 让我知道您是否需要更多数据/信息。
By returing a binder in onBind you are creating a bound service, which means it is "bound" to your app. 通过重载onBind中的绑定程序,您可以创建绑定服务,这意味着该服务已“绑定”到您的应用程序。 This means that it get's destroyed when you app does, that explains the home button destroy. 这意味着您在应用程序运行时会销毁它,这说明主页按钮销毁了。 You should be returning null for a background service. 您应该为后台服务返回null 。
@Override
public void onCreate() {
//keeps being called thus I lose my data
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
You should start your service with START_STICKY
flag, so that if the OS destroys your service when running low on resources, it will later recreate it. 您应该使用START_STICKY
标志启动服务,这样,如果操作系统在资源不足时破坏了服务,则以后会重新创建它。
Prior to destroying your service, Android will call public void onLowMemory()
, there save any data you need so that when it restarts your service, you will be able to do your task again. 在销毁服务之前,Android会调用public void onLowMemory()
,在其中保存您需要的任何数据,以便在重新启动服务时能够再次执行任务。
EDIT: As per your comment, you would like a service that is running in the background and also allows binding. 编辑:根据您的评论,您想要在后台运行并且还允许绑定的服务。 You can do that if you call the startService()
method before any bindService()
calls are made... this will effectively make your service a started service instead of bound. 如果在进行任何bindService()
调用之前调用了startService()
方法,则可以执行此操作……这将有效地使您的服务成为已启动的服务,而不是绑定的。 In that case you are able to return a IBInder
and bind to it. 在这种情况下,您可以返回IBInder
并绑定到它。
There is a really, really good article HERE 有一个非常,非常好的文章HERE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.