Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

null intent redelivered to Service onStartCommand()

In the Android documentation, the Service's "onStartCommand()" has an intent given as a param, that according to the docs:

"the Intent supplied to startService(Intent), as given. This may be null if the service is being restarted after its process has gone away, and it had previously returned anything except START_STICKY_COMPATIBILITY."

However, the return value START_REDELIVER_INTENT is supposed to return the original intent when restarting a service.

Can anyone explain why an intent can be null, even if the flag was set to START_REDELIVER_INTENT?

like image 731
Yuxal Avatar asked Dec 08 '15 07:12

Yuxal


People also ask

What should onStartCommand () return incase if the service gets killed by OS?

Constant to return from onStartCommand(Intent, int, int) : if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int) ), then it will be scheduled for a restart and the last delivered Intent re-delivered to it again via onStartCommand(Intent, int, int) .

What is sticky and non sticky in Android?

START_STICKY. the system will try to re-create your service after it is killed. START_NOT_STICKY. the system will not try to re-create your service after it is killed. Standard example: @Override public int onStartCommand(Intent intent, int flags, int startId) { return START_STICKY; }

What is a sticky service?

Sticky Services — Sticky service is somewhere between regular service and foreground service Android will kill the process time to time of this service, however it will be created automatically if the resources are available as soon as possible.

What happens when you start a service in Android?

Starting a serviceThe Android system calls the service's onStartCommand() method and passes it the Intent , which specifies which service to start. Note: If your app targets API level 26 or higher, the system imposes restrictions on using or creating background services unless the app itself is in the foreground.


1 Answers

Are you possibly confusing START_FLAG_REDELIVERY with START_REDELIVER_INTENT? Your post says, "the return value START_FLAG_REDELIVERY". That constant is not one of the values returned from onStartCommand, it is one of the bit values passed into onStartCommand as the flags parameter. START_FLAG_REDELIVERY and START_STICKY both have value of 1. If you mistakenly have return START_FLAG_REDELIVERY at the end of onStartCommand(), the service will restart in sticky mode with a null intent if there are no start commends pending.

like image 72
Bob Snyder Avatar answered Sep 20 '22 16:09

Bob Snyder