我开发中后台服务创建,收集传感器数据的应用程序。我从我的活动启动服务:
startService(新意图(这一点,MyService.class));
我创建的服务,所以如果应用程序被破坏,后台服务仍然继续收集数据。我想这一点,和它的工作在一定程度上。我的问题是,当我杀了应用程序,该服务似乎重新启动,因为的onCreate()
服务和 ONSTART()
方法被调用。是否有与该服务没有重新启动吗?
更新:
所建议在下面一个答案,我在服务但没有运气增加了以下方法
@覆盖
公众诠释onStartCommand(意向意图,诠释标志,诠释startId){
返回START_NOT_STICKY;
}
解决方案
这取决于onStartCommand返回的值。
您必须返回START_NOT_STICKY
按照 DOC :
有关开始服务,有两种操作额外主要模式,他们可以决定运行中,这取决于他们从onStartCommand()返回的值:START_STICKY用于显式开始,并根据需要停止的服务,而START_NOT_STICKY或START_REDELIVER_INTENT用于在处理发送给他们的任何命令,只应保持运行的服务
在短期: 如果返回START_STICKY服务被重新只要资源可用。如果返回START_NOT_STICKY你不得不重新启动该服务发送一个新的意图。
由于这一切引发了我的好奇心,我做了一个示例应用程序进行测试。你可以找到所有的这里来源的zip 有一个startService按钮和一个stopService按钮,你会期望从他们什么。 该服务将返回START_NOT_STICKY在onStartCommand。 我放在的onCreate,onStartCommand和的onDestroy祝酒词。
下面会发生什么:
如果我preSS开始的onCreate和ONSTART被称为 如果我preSS停止,的onDestroy被触发 如果我preSS启动两次,的onCreate被调用一次,onStartCommand两次所以它表现为人们所期望的。
如果我启动该服务,并杀死的应用程序,你描述的,的onDestroy不会被调用,但没有的onCreate或ONSTART。
如果我回到了应用程序,我preSS重新开始,的onCreate被称为这意味着,正如我以前写的,START_NOT_STICKY prevents服务,以获得自动重新启动。
我想你有一个重新开始的服务(可能是悬而未决的意图)别的东西在你的应用程序。
I am developing an application in which a background service is created to collect sensor data. I am starting the service from my activity:
startService(new Intent(this, MyService.class));
I created the service so if the application is destroyed, the background service still continues to collect data. I tried this, and it worked to a certain extent. My problem is that when I kill the application, the service seems to restart because the onCreate()
service and the onStart()
methods are invoked. Is there any way with which the service isn't restarted please?
UPDATE:
As suggested in an answer below, I added the following method in the service but no luck.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_NOT_STICKY;
}
解决方案
It depends on the value returned in onStartCommand.
You must return START_NOT_STICKY
According to the doc:
For started services, there are two additional major modes of operation they can decide to run in, depending on the value they return from onStartCommand(): START_STICKY is used for services that are explicitly started and stopped as needed, while START_NOT_STICKY or START_REDELIVER_INTENT are used for services that should only remain running while processing any commands sent to them
In short: If you return START_STICKY the service gets recreated whenever the resources are available. If you return START_NOT_STICKY you have to re-activate the service sending a new intent.
Since all of this triggered my curiosity, I made a sample app to test this. You can find the zip with all the sources here There are a startService button and a stopService button that do what you would expect from them. The service returns START_NOT_STICKY in onStartCommand. I placed toasts in onCreate, onStartCommand and onDestroy.
Here what happens:
If I press start, onCreate and onStart are called If I press stop, onDestroy is triggered If I press start twice, onCreate is called once and onStartCommand twiceSo it behaves as one would expect.
If I start the service and kill the app as you described, onDestroy does not get called but neither onCreate or onStart.
If I get back to the app and I press start again, onCreate gets called which means that, as I wrote before, START_NOT_STICKY prevents the service to getting restarted automatically.
I guess you have something else in your app that starts the service again (maybe a pending intent).
相关推荐
最新文章