Android passing data from Activity to BroadcastReceiver shows null

I have an activity that uses AlarmManager to call BroadcastReceiver at a specific point in time. All this works fine, except when I try to add some extra lines to the intent when calling BroadcastReceiver, they always appear as null on the other end.

Operation 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 BroadcastReceiver, what am I doing wrong?

+4
source share
1 answer

Try to get it with

 intent.getExtras().get("testString"); 
+7
source

Source: https://habr.com/ru/post/1313965/


All Articles