Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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; } 
like image 480
duncanportelli Avatar asked Mar 16 '13 18:03

duncanportelli


People also ask

How run service in background android when app is killed?

First, the easiest way to do what you're trying to do is to launch an Android Broadcast when the app is killed manually, and define a custom BroadcastReceiver to trigger a service restart after that. Dear Dr Sabri Allani, If your Service is started by your app then actually your service is running on main process.

How do you stop foreground service when an app is killed?

1)Keep it running in the foreground by calling the startForeground() method. Show activity on this post. Show activity on this post. You can use android:stopWithTask="false" in manifest as bellow, This means even if user kills app by removing it from tasklist, your service won't stop.

How do I turn off background services on android?

Stop Services Running in BackgroundOpen Settings of the phone. Now, go to the Developer Options. Tap on Running Services. Tap on the app for which you want to limit battery usage, now tap on stop.

How do I find out what background services are running on my android?

You can do this by making your own Interface where you declare for example " isServiceRunning() ". You can then bind your Activity to your Service, run the method isServiceRunning(), the Service will check for itself if it is running or not and returns a boolean to your Activity.


1 Answers

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

like image 176
fedepaol Avatar answered Sep 22 '22 13:09

fedepaol