Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
296 views
in Technique[技术] by (71.8m points)

Android Background Service is restarting when application is killed

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It depends on the value returned in onStartCommand.

You must return START_NOT_STICKY

According to the documentation:

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 twice

So 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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...