I have an Android application.
In one activity I start a service like this:
Intent startIntent = new Intent(_context, HandlingService.class);
_context.startService(startIntent);
HandlingService is defined as follows:
public class HandlingService extends IntentService
Then inside HandlingService I have this:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, startId, startId);
Log.v(ApplicationName,"HandlingService.onStartCommand");
if ((flags & START_FLAG_RETRY) == 0){
Log.v(ApplicationName,"Service is restarting");
}
return START_STICKY;
}
and this:
@Override
protected void onHandleIntent(Intent sourceIntent) {
Log.v(ApplicationName, "HandlingService.onHandleIntent");
sendSomething();
}
and lastly:
protected void sendSomething() {
while (numberOfTry > 0) {
numberOfTry++;
Log.v(ApplicationName,"HandlingService.sending Something. Try# " + numberOfTry);
}
}
numberOfTry starts in 1.
Then from the activity that starts the service, when I click on a cancel button I call this:
Intent stopIntent = new Intent(CallingActivity.this, HandlingService.class);
CallingActivity.this.stopService(stopIntent);
I see HandlingService.OnDestroy being called, but I keep seeing the log with “HandlingService.sending Something. Try# ” and the increasing numbers.
Question: Why it keeps alive if I already stopped it with the call to stopService?
Thanks in advance! Guillermo.
The documentation for IntentService explicitly states that you should not override
onStartCommand()in your derived class. You are probably messing up the internal mechanics of theIntentService.Either derive your class directly from
Serviceor use the framework thatIntentServicealready gives you (for starting/stopping itself).