2

I'm trying to launch a service from another service with certain extras. However, I can't retrieve those extras in the launched service, .getIntExtra returns a NullPointerException.

This is how I launch the service:

Intent serviceIntent = new Intent(context, RefreshService.class);
serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);

PendingIntent contentIntent = PendingIntent.getService(this, 0, serviceIntent, 0);
updateViews.setOnClickPendingIntent(R.id.btnRefresh, contentIntent);


And this how I'm trying to receive the extras in the started service:

@Override    
public void onStart(Intent intent, int startId){
    super.onStart(intent, startId);
    mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
}

In my example, mAppWidgetId always resorts to the default value -1.

What am I doing wrong?


Thanks for your help,

Nick

Nick
  • 3,504
  • 2
  • 39
  • 78

2 Answers2

1

I "solved" it by transmitting my custom values through .setAction and .setFlags instead of .putExtras, which works fine. I can retrieve these with .getAction and .getFlags

Nick
  • 3,504
  • 2
  • 39
  • 78
0

Sounds suspiciously like this: Android keeps caching my intents Extras, how to declare a pending intent that keeps fresh extras?

Community
  • 1
  • 1
EboMike
  • 76,846
  • 14
  • 164
  • 167
  • Thanks for your answer, but in my case, no extra at all is (seemingly) passed to the started service, it's not a question of which is the most recent one. I tried adding the FLAG_UPDATE_CURRENT though, but that doesn't help :-/ – Nick Jul 24 '10 at 23:34
  • ACtually, wait, those are two conflicting statements - at first, you say getIntExtra throws a NPE (indicating that intent is null), but then you say that it returns -1 (indicating that either the extra doesn't exist, or you actually pass in -1 - did you verify that you pass in the right value?). – EboMike Jul 24 '10 at 23:40
  • If you look at line four of the second code-block: getIntExtra resolves to "-1", as this has been specific as the default value (in case that the intent extra doesn't exist). If I would query the extra without specifying a default value, I would get a NPE. – Nick Jul 25 '10 at 12:39