views:

19

answers:

1

So I understand the service lifecycle and all that. But I'm confused what the startId parameter is for?

public int onStartCommand (Intent intent, int flags, int startId)

I get that it's used in conjunction with stopSelf(int), but I don't see what the point is or where the startId is being generated. What use case would using stopSelf(int) fall under?

+1  A: 

What use case would using stopSelf(int) fall under?

Step #1: Call startService()

Step #2: Call startService() again

Step #3: Call stopSelf()

At this point, we want the service to not be stopped, since there is still work outstanding, represented by the second startService() call. We want the startService() and stopSelf() calls to match.

CommonsWare
Oh, I think I get it. I think I'm overthinking this. So we would just store startId to a global variable whenever onStartCommand() gets executed, and then call stopSelf(int) on that global variable. Ok gotcha. I was trying to thinking about putting the IDs into a synchronous queue or something lol
Falmarri
@Falmarri: "So we would just store startId to a global variable whenever onStartCommand() gets executed" -- no, since there will be several of these. You may wish to examine `IntentService` and consider whether that is a better base class for you to use. It handles the `stopSelf(int)` calls itself, among other features.
CommonsWare
Hmm, then I guess I'm still confused. How would you go about handling it yourself? Is there an example? I haven't seen any use of the startId in any examples
Falmarri
@Falmarri: You pass `startId` as a parameter around your code, until you get to the point where you need it for `stopSelf()`.
CommonsWare