jackbot
jackbot

Reputation: 3021

Android passing data from Activity to BroadcastReceiver shows null

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?

Upvotes: 4

Views: 3821

Answers (1)

Macarse
Macarse

Reputation: 93143

Try getting it with:

intent.getExtras().get("testString");

Upvotes: 7

Related Questions