Martin
Martin

Reputation: 24308

Android: When onStartCommand is finished, onDestroy isn't created

Can anyone help. I have built a service that executes with the BOOT_COMPLETED intent, hence my service is started when the device is booted. n In my onStartCommand i am registering the service with the AlarmManger to execute every 15 minutes.

This works but i notice that my service is never stopped hence when onStartCommand finished - it never stops, the onDestroy is NEVER called.

I need to return an integer from onStartCommand which i believe controls the life time..

What integer should i return?

What am i doing wrong?

Thank in advance

EDIT

Currently the serive returns super.onStartCommand(......

which is by default. So what is being returned here?

And if i remove it should i not call (rather than return) to super.onStartCommand on the first line on my onStartCommand in my class?

Upvotes: -1

Views: 2312

Answers (2)

waqaslam
waqaslam

Reputation: 68177

When you call onStartCommand(), it only passes intent to your already created service. It is not supposed to kill itself after the method is executed.

To kill your service, use stopSelf() and then it will call onDestroy() method of your service.

You may return any of the following value depending on your requirements in onStartCommand:

  • START_CONTINUATION_MASK
  • START_FLAG_REDELIVERY
  • START_FLAG_RETRY
  • START_NOT_STICKY
  • START_REDELIVER_INTENT
  • START_STICKY
  • START_STICKY_COMPATIBILITY

For more info, read Android Service carefully.

Upvotes: 1

Huang
Huang

Reputation: 4842

The SDK document of Service lifecycle say that

The service will at this point continue running until Context.stopService() or stopSelf() is called

For the return value of onStartCommand (Intent intent, int flags, int startId), the document says that

The return value indicates what semantics the system should use for the service's current started state. It may be one of the constants associated with the START_CONTINUATION_MASK bits.

and

public static final int START_CONTINUATION_MASK Since: API Level 5

Bits returned by onStartCommand(Intent, int, int) describing how to continue the service if it is killed. May be START_STICKY, START_NOT_STICKY, START_REDELIVER_INTENT, or START_STICKY_COMPATIBILITY.

So, if you want to stop your service, you should call Context.stopService() or Service.stopSelf().

Upvotes: 0

Related Questions