views:

53

answers:

1

I've got an activity which uses an AlarmManager to call a BroadcastReceiver at a particular point in time. This all works fine, except when I try to add some extra strings to the intent when calling the BroadcastReceiver, they always come up as null on the other end.

Activity code:

    Intent intent = new Intent(this, ScheduleReceiver.class);
    intent.putExtra("testString", "I'm a string");
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 999, intent, 0);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC, System.currentTimeMillis(), pendingIntent);

BroadcastReceiver code

 public void onReceive(Context context, Intent intent) {
      Log.v(TAG, "TestString: " + intent.getStringExtra("testString"));
 }

The content of 'teststring' is always null in the BroadcastReceiver, what am I doing wrong?

+1  A: 

Try getting it with:

intent.getExtras().get("testString");
Macarse
I think it was a combination of this and the fact I added the `PendingIntent.FLAG_CANCEL_CURRENT` flag to the PendingItent.
jackbot