0

In my app I make use of a background service to do some background data fetching. I launch the service using

bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

mConnection is instance of ServiceConnection.

In service I keep fetching some data and validate it, and if the check passes I play a sound.So when this happens user can press a STOP button on activity screen.This would call unbind(mConnection),which would destroy the service wherein I stop the sound. My query is that while service is running in background, and meanwhile Android kills the activity then how would user be able to stop the service.I need mConnection to unbind the service and I can't store mConnection in Bundle in onSaveInstanceState.

Mandroid
  • 6,200
  • 12
  • 64
  • 134
  • Would it not be better to use an IntentService, and a Broadcast Receiver (startService call) rather than a bound service as it's only a specific background task you want to do, rather than lots of different tasks? – Mark Jul 31 '16 at 11:42
  • Hi Mark, I want to maintain activity UI so that if user wants to stop background service in between then he/she can do it with STOP button. IntentService,once launched, won't be in user's control. – Mandroid Jul 31 '16 at 11:53
  • Why not use an Executor/Threadpool/Runnable/ in a service, that way you can still have control by sending a command to shutdownNow() on the Executor - that way the service is not bound to the Activity life cycle and you can maintain control of the Executing Thread in the service. – Mark Jul 31 '16 at 11:59
  • and what is your problem actually? – pskink Jul 31 '16 at 12:23
  • Hi pskink, I am worried about a scenario when android system kills the client activity. So it would kill binder service too. I want to have service keep running even if client activity is killed by OS and when user comes back to the activity he/she gets it in original state so that user can interact with it to stop the service, if needed.. That is, service lifecycle is not impacted by activity's lifecycle. – Mandroid Jul 31 '16 at 13:12
  • just call `starService` before calling `bindService` and when your activity calls `unbindService` the service will be still in "started" state, more [here](https://developer.android.com/guide/components/bound-services.html), search for `Binding to a Started Service` – pskink Jul 31 '16 at 13:33

1 Answers1

1

If you start the service using bindService(...) method, then when the activity is killed by the system so does the service. Its' lifecycle is bound to the activity. BTW the system will kill the process and not only the activity. This is different when you start services with startService() and START_STICKY.

In your case you don't have to worry. As long as the activity stops, so does your service.

georgeok
  • 5,321
  • 2
  • 39
  • 61
  • My objective is that activity keeps working even if parent activity is killed by Android, and if user revisits the activity then it is restored in same UI state as earlier. Is it doable? – Mandroid Jul 31 '16 at 11:54