Reasons that the passed Intent would be NULL in onStartCommand

26,996

Solution 1

I'm surprised there's no discussion of the incoming flags. I'm going to monitor this in the logs with the following:

if (null == intent || null == intent.getAction ()) {
        String source = null == intent ? "intent" : "action";
        Log.e (TAG, source + " was null, flags=" + flags + " bits=" + Integer.toBinaryString (flags));
        return START_STICKY;
}

Update: Flags were 0 so there was nothing actionable there. I've left the null check in there with no loss of function.

Edit: Ok, I found it in the documentation of START_STICKY of all places! "if there are not any pending start commands to be delivered to the service, it will be called with a null intent object, so you must take care to check for this."

http://developer.android.com/reference/android/app/Service.html

Solution 2

Return START_REDELIVER_INTENT in your callback:

    public int onStartCommand(Intent intent, int flags, int startId) {

        // your service code here

        return android.app.Service.START_REDELIVER_INTENT; // make sure restart delivers the intent
    }

It's an unexpected default, most of the time you don't want START_STICKY but START_REDELIVER_INTENT. START_REDELIVER_INTENT is the expected behavior.

Share:
26,996

Related videos on Youtube

rf43
Author by

rf43

I am just a dude...

Updated on July 21, 2022

Comments

  • rf43
    rf43 almost 2 years

    Is there any other reason that the Intent that is passed to onStartCommand(Intent, int, int) would be NULL besides the system restarting the service via a flag such as START_STICKY?

    Also, when the service is restarted by the system the Intent.getAction() method returns NULL... sometimes. Intent is not NULL just getAction()

    I asked here too but haven't received an answer just yet.

    UPDATE: After chatting with Mark Murphy, he suggested that I return START_REDELIVER_INTENT in the onStartCommand() callback in my service instead of START_STICKY so that the entire intent is sent following a restart.

    I didn't do this initially because I was concerned that if the service was attempting to do something, then in the middle of that something the service was restarted... will it recognize that it started doing that something? I guess that is logic I will need to be responsible for :)

    • Dale Wilson
      Dale Wilson about 11 years
      Rather than editing your question with the answer, please add an answer and accept it so your question will stop showing up in the set of Unanswered questions -- thanks.
    • Piotr Chojnacki
      Piotr Chojnacki almost 11 years
      Just a note for someone with similar problem. I found that usually when I get error that intent is null on onStartCommand(), it is caused by some other error which is visible in LogCat before this. I don't know why, but this is what I observed and it's quite easy to overlook it.
    • rf43
      rf43 almost 11 years
      @DaleWilson I would but this question isn't truly resolved. Until I receive a definitive answer as to why an Intent is null or how to avoid a null intent without resorting to using START_REDELIVER_INTENT (which, I later learned, is not what I needed but will still solve some people's issues thus I left my edit) I cannot accept an answer.
    • rf43
      rf43 almost 11 years
      @Mosquito Did you happen to notice what, specifically, was throwing an error?
    • Piotr Chojnacki
      Piotr Chojnacki almost 11 years
      @DDoSAttack Doesn't really matter. In my case it was for example NullPointerException somewhere in one of Activities. Later it was some other error. But in both cases my stack trace showed that my service - which was running while this error occured - had intent that was null as in your case. I was thinking quite a long time what's wrong, when I decided to scroll up the stack trace and it appeared that somewhere at the top I had my true error. After solving this one, also the one with null intent disappeared.
    • Eric Woodruff
      Eric Woodruff over 10 years
      I'm surprised there's no discussion of the incoming flags when it starts with a null intent. It seems like the system should communicate something about why it started this way.
    • likejudo
      likejudo about 9 years
      @PiotrChojnacki exactly right +1. I scrolled up and found another error that causes the process to be killed.
  • Muhammad Naderi
    Muhammad Naderi about 6 years
    in kotlin, the intent is marked as not-null, and it crashes the app override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {}
  • Patrick Boos
    Patrick Boos almost 6 years
    @MuhammadNaderi you can just make intent to Intent?. Worked for me. But then of course you need to correctly handle null intent.
  • Dr.jacky
    Dr.jacky about 5 years
    @PatrickBoos But then of course you need to correctly handle null intent; What do you mean?! How? Thanks.
  • Patrick Boos
    Patrick Boos about 5 years
    I meant just in your code make onStartCommand(intent: Intent?, ...) it should work. And then just don't do intent!! but correctly check if intent is null.