2

If you start a service by calling startService() then you have to call stopService() or stopSelf() to stop the service. If you want to stop a service after doing some work, you might want to use IntentService instead.`

If I am NOT using IntentService then when will the service stop if I don't call the stopService() or stopSelf() methods?

vinod
  • 2,358
  • 19
  • 26
user3684678
  • 451
  • 3
  • 7
  • 20
  • 2
    only if the system needs resources or a taskmanager kills Your app/service... – Opiatefuchs Feb 19 '16 at 11:13
  • @Opiatefuchs And if i closes the app then also service will keep running? – user3684678 Feb 19 '16 at 11:14
  • yes, service keeps running until one of the cases that I wrote comes down. .. – Opiatefuchs Feb 19 '16 at 11:16
  • @Opiatefuchs If i switch off my device and then switch on then also will service keeps running? – user3684678 Feb 19 '16 at 11:17
  • What You mean by switch on and off? Do You mean like reboot or do You mean the screen? By reboot, then the service stops. By screen, it depends on the device. For example Huawei has an energy manager inside, that stops apps by going to sleep if they are not protected. It´s like a implemented task manager. But not all devices behaving like this. – Opiatefuchs Feb 19 '16 at 11:19
  • "A service can kill itself by using stopSelfResult(resultid) method."..when is that method called?is it called automatically? – user3684678 Feb 19 '16 at 11:20
  • @Opiatefuchs By switch off i mean device is switched off or rebooted.. – user3684678 Feb 19 '16 at 11:21
  • @Opiatefuchs If you kill the app, your service will be terminated. By default the service runs in the UI thread of your app (you may restart it later though). Only a "bound" service/process will keep running after the app dies. – Ross Stepaniak Feb 19 '16 at 11:21
  • when the app process is being killed either by user or by system! – Muhammad Babar Feb 19 '16 at 11:21
  • i see contradictory answers here..one is saying service keeps running after killing app until system needs resources or a taskmanager kills Your app/service and another saying service will be terminated on killing app... – user3684678 Feb 19 '16 at 11:24
  • @Ross....well....I don´t think he not mean with "Kill the app" really kill, I think he just means, closing like pressing back button. And then, the service is not killed..... – Opiatefuchs Feb 19 '16 at 11:24
  • @Opiatefuchs And if app is not in background will service stop? – user3684678 Feb 19 '16 at 11:25
  • @user3684678...for clearance: If You talk about "killing" your app, did You mean just close it like with back button press? If yes, the service gets not killed, only if You really kill Your app like with a task manager. And stopSelf is not automatically called, You have to put it inside Your service. Just call stopSelf() after service has done it´s work. – Opiatefuchs Feb 19 '16 at 11:27
  • @Opiatefuchs Thanks..is there any way to keep service running even after killing app?Suppose I am downloading 500 MB file in sevice and i want to download to keep in progress even after killing app? – user3684678 Feb 19 '16 at 11:29
  • no, if Your app is really killed, the service will definetely stopped. You have some possibilities to prevent Your app from being killed, for example with a foreground service. But there is no guarantee that even with this foreground service, Your app is not beeing killed. But this kind of service will keep it alive as long as possible, in some rare cases, when System needs urgently resources, also a foreground service gets stopped. – Opiatefuchs Feb 19 '16 at 11:32
  • The Service (unless its “bound”) will stop in case you kill the app (kill = remove from tray completely) The service restarts and loses all the previously logged data 2. The Service will keep on running in case you send the app to the background. 3. You may run your service in a different process than your Application (to make sure) the service survives after the app is killed: – Ross Stepaniak Feb 19 '16 at 11:38

1 Answers1

2

If I am NOT using IntentService then when will service stop if I don't call stopService() or stopSelf() methods?

If your service was started via startService(), and you do not call stopService() or stopSelf(), your service will be considered "running" until your process is terminated. You may or may not get called with onDestroy() as part of this, depending on how urgent the need is for your process' system RAM.

If, from onStartCommand(), you returned START_STICKY or START_REDELIVER_INTENT, your process and service is supposed to be restarted automatically at some time in the not-too-distant future, when RAM needs permit it.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • When will process gets terminated?If i kill my app then the service will be terminated? – user3684678 Feb 21 '16 at 07:08
  • @user3684678: Your process will be terminated [sometime after it moves to the background](http://developer.android.com/guide/components/processes-and-threads.html#Lifecycle), or if the user requests it (e.g., kills via task manager). – CommonsWare Feb 21 '16 at 12:39
  • if i am using android:stopWithTask="false" and not calling stopService() or stopSelf() methods in a started service,when will service stop if application is cleared from Recent app? – Android Developer Jan 04 '21 at 10:16
  • @AndroidDeveloper: I expect that will depend a lot by device manufacturer. – CommonsWare Jan 04 '21 at 11:57