开发者

Android passing data from Activity to BroadcastReceiver shows null

开发者 https://www.devze.com 2023-01-05 07:22 出处:网络
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

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

精彩评论

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

关注公众号