2

I am confused:

Is intent.getExtras.getInt() same as intent.getIntExtra()?

If I start my service using START_REDELIVER_INTENT, will the extras be included in the intent?

I get NullPointerException on restart of my crashed service, which I find strange....

Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
JohnyTex
  • 3,323
  • 5
  • 29
  • 52

2 Answers2

4

From Intent source code :

private Bundle mExtras;

// [...]

public int getIntExtra(String name, int defaultValue) {
    return mExtras == null ? defaultValue :
    mExtras.getInt(name, defaultValue);
}

public Bundle getExtras() {
    return (mExtras != null)
        ? new Bundle(mExtras)
       : null;
}

So yes. Same thing except getExtras() may return null.

Adrian
  • 2,911
  • 1
  • 17
  • 24
ToYonos
  • 16,469
  • 2
  • 54
  • 70
  • 1
    In order to return a copy of the Bundle, then, the original mExtras Bundle remains not modified. – ToYonos Oct 13 '14 at 11:32
1

They are not quite identical. As you are finding out, the first variant will cause a NPE if intent.getExtras() returns null. The second variant does its own null-checking, and returns the default value if the extra is not present.

I can't speculate as to why you aren't getting the expected Extras without seeing more code.

Graham Borland
  • 60,055
  • 21
  • 138
  • 179
  • I understand. Though it is the expected behaviour that the extras get redelivered as well on restart of service? Correct? – JohnyTex Oct 13 '14 at 11:36