Android passing data from Activity to BroadcastReceiver shows null

Deadly 提交于 2019-12-10 16:32:31

问题


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:


Try getting it with:

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


来源:https://stackoverflow.com/questions/3125009/android-passing-data-from-activity-to-broadcastreceiver-shows-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!