0

I have a Service which is started from BroadcastReceiver when a new incoming call arrives. In that Service I start a new Thread and do all the work there. What happens if the Main Activity of my app is killed? I'm asking because I realised that in such a situation the Service's startID rises by one! Just like if the Service's onStartCommand() was called again or something like that. I'll give a short example:

  1. Main Activity is running somewhere in the background, incoming call arrives, Service starts, stopSelf() is called by the user action, Service is destroyed.
  2. Main Activity is running, incoming call arrives, Service starts, Main Activity is killed by clearing Recent apps list, stopSelf() is called by the user action, Service is NOT destroyed because startIDs don't match.
  3. Main Activity is NOT running (killed by clearing Recent apps list), incoming call arrives, Service starts, stopSelf() is called by the user action, Service is destroyed.

That is very strange, because startID somehow changes its value when the Main Activity is killed. I use START_NOT_STICKY in my Service's onStartCommand() method so there is no way that Service is recreated with a new startID. Could someone explain this to me? What happens to the Service or the separate Thread where the service is running when Main Activity is killed? Thanks in advance!

Salivan
  • 1,876
  • 7
  • 26
  • 45
  • 1
    Activities aren't "killed". An activity will be destroyed if the users presses BACK, you call `finish()`, etc. A process will be terminated (a.k.a., killed), by user action (e.g., swiping the app off the recent-tasks list) or by an OS need to free up system RAM. – CommonsWare Apr 17 '14 at 19:12
  • But if my service runs in the same process as my activity, will the process be killed in such a situation? Because now it's not. The service keeps running and becomes unstoppable because the `startID`s don't match... – Salivan Apr 17 '14 at 19:16
  • "But if my service runs in the same process as my activity, will the process be killed in such a situation?" -- a service does not prevent a process from being killed. It lowers the priority of killing it, compared to other processes, for when the OS needs to free up system RAM. But your process can still be killed by user action as well. – CommonsWare Apr 17 '14 at 19:21
  • But it isn't. Even if the main activity is destroyed, the process still runs and the service becomes unstoppable. Please read my provided example. – Salivan Apr 17 '14 at 19:28

1 Answers1

1

Removing an activity from "recents" does not kill it. If the incoming call broadcast results in a "startService" call, a service's "onStartCommand" will be called again without a problem (Android onCreate or onStartCommand for starting service).

So, in #2 you might think your activity "died" but it didn't, and since startId changes every time the onStartCommand is called, the next request to start the service won't match any previous startId. This is the expected behavior.

Community
  • 1
  • 1
Jim
  • 10,172
  • 1
  • 27
  • 36
  • You misunderstood me I guess. In #2, the service is not running, startid is 0 let's say, then call arrives, then service is started and startid is 1, then I clear the activity from recents and somehow startid value changes to 2. No one calls me, but it somehow changes to 2. Even logs show me that it's 1, but after I clear recents, I'm only able to stop the service with startid value 2... By the way, in my app the activity and the service are totally separate thjng – Salivan Apr 18 '14 at 08:45