Search code examples
androidandroid-serviceandroid-pendingintentrestartsticky

What does it mean "Do not recreate the service, unless there are pending intents to deliver."?


I handle Service and the return code: START_NOT_STICKY. I do not want to restart the service.

But documentation says "Do not recreate the service, unless there are pending intents to deliver." Could you give me an example of these pending intents that cause restarting the service?


Solution

  • When you return START_NOT_STICKY, this means the following:

    If Android kills the process hosting your Service (which it can pretty much do at any time if it needs the resources or if it thinks your Service isn't doing anything useful), the following happens:

    • If your process is killed after onStartCommand() is called, but before onStartCommand() has completed, Android will restart your Service and call onStartCommand() again, redelivering the Intent that was being processed when the process was killed

    • If your process is killed after onStartCommand() has completed, Android will only restart your Service if there are pending Intents for your Service. In this case a pending Intent would exist if any component called startService() for your Service and that call has not yet been completely processed by your Service. This could be the case, for example, if a component called startService() while your Service was dead. Or it could happen if a component called startService() while your Service was still in the onStartCommand() method (processing a previous call to startService()).