Reputation: 11156
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:
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 !!
Upvotes: 0
Views: 1938
Reputation: 192023
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 ofSTART_FLAG_REDELIVERY
orSTART_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 withstopSelfResult(int)
.
So, the id is used for logging and getting a handle to the service to stop it
Upvotes: 1