0
votes

with updated SDK version now Service class has override method

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

& it has replaced following which was introduce with Android 2.0 (API level 5).

void onStart(Intent intent, int startid)

My question is:

  1. what is use of params like flag & startId passing as an argument?
  2. how we can manipulate these params ?

Update 1. 2: Clear :)

Following snapcode clear use case of flags param. Default it received as 0, in case if your service started back then it will be received with flag what we return from onStartCommand(..) method.

void readFlags(int flags) {
    switch (flags) {
        case START_FLAG_REDELIVERY:
        case START_FLAG_RETRY:
            // restarted by system, might be kill app form stack.
            break;

        default:
            // on regular startService call from client.
    }
}

and startID is pretty much clear, whenever you call stopSelf you should call with this startID, so in case if service has running request by multiple client then it will not kill service, it will simply stop work for this startID. it's generated by system no need to manipulate :).

3. But how to manage this startID to call in stopSelf is still an question ? Any one !!

1
Did you read the Android documentation for those parameters?OneCricketeer
yes..definitely but not getting use of flag & startId. And how we can manipulate?CoDe

1 Answers

1
votes

Firstly, documentation explicitly says not to call these methods yourself, therefore you would manipulate them elsewhere.

flags int:
Additional data about this start request. Value is either 0 or combination of START_FLAG_REDELIVERY or START_FLAG_RETRY.

Those constant flags are described in the same page

It's not clear to me where that input comes from, but you combine that integer with bitwise OR, for example

 return flags | START_REDELIVER_INTENT; 

And

startId int:
A unique integer representing this specific request to start. Use with stopSelfResult(int).

So, the id is used for logging and getting a handle to the service to stop it