简体   繁体   English

应用程序被杀死然后重新启动时,Android服务(在其自己的线程中)死亡

[英]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: 我有两个问题:

  • First of all, when the user closes the application (by holding the middle button and close the app) the service is destroyed, which means, I lose my data (I'm assuming it gets destroyed as it reboots automatically). 首先,当用户关闭应用程序时(按住中间按钮并关闭应用程序),服务被破坏了,这意味着我丢失了数据(我假设它在自动重启时被破坏了)。
  • 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.

相关问题 android服务在应用程序被杀死时自动重启 - android service restarts itself on application killed 当我在Android中使用绑定服务时,它是否在后台,在其自己的线程上或在主UI线程上运行? - When I use a Bound Service in Android, does it run in the background, on its own thread or on the main UI thread? 启动时服务线程死 - Service Thread Dies on Start Java-当子线程死于NPE时,父线程被杀死会发生什么? - Java - What happens when child thread dies from NPE , does parent thread get killed Android如何在用户杀死应用程序后继续运行后台服务? - Android How to keep Running the background service when app is killed by user? Android 应用程序被杀死时服务停止录制音频 - Android Service stops recording audio when app is killed android中手机重启时重启服务 - Restart the service when phone restarts in android 每当应用在Android Pie中被杀死时,服务也会被杀死 - Service also gets killed whenever App is killed in android pie 一个进程是否在它创建的线程死亡时死亡? - Does a process dies when the thread it created dies? Android - 服务,IntentService,JobIntentService - 如果应用程序被杀,它们将被停止 - Android - Service, IntentService, JobIntentService - they are stoped if app killed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM