开发者

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 conten开发者_JS百科t of 'teststring' is always null in the BroadcastReceiver, what am I doing wrong?


Try getting it with:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜