Android cannot pass intent extras though AlarmManager

AndroidBroadcastreceiverAlarmmanagerAndroid PendingintentAndroid Bundle

Android Problem Overview


I am trying to put an extra message in my intent to pass to AlarmManager to be triggered at a later time. My onReceive triggers correctly but extras.getString() returns null

Setup:

public PendingIntent getPendingIntent(int uniqueRequestCode, String extra) {
	Intent intent = new Intent(this, ActionReceiver.class);
	intent.putExtra("EXTRA", extra);
	PendingIntent pendingIntent = PendingIntent.getBroadcast(this, uniqueRequestCode,
			intent, 0);
	return pendingIntent;
}

public void setSilentLater(TimeRule timeRule) {
	boolean[] weekdays = timeRule.getReoccurringWeekdays();
	int dayOfWeek = 0;

	for (boolean day : weekdays) {
		dayOfWeek++;
		if (day == true) {
			Calendar cal = Calendar.getInstance();

			AlarmManager alarmManager = (AlarmManager) this
					.getSystemService(Context.ALARM_SERVICE);

			cal.set(Calendar.DAY_OF_WEEK, dayOfWeek);
			cal.set(Calendar.HOUR_OF_DAY,
					timeRule.getStartTime().get(Calendar.HOUR_OF_DAY));
			cal.set(Calendar.MINUTE,
					timeRule.getStartTime().get(Calendar.MINUTE));
			cal.set(Calendar.SECOND, 0);
			cal.set(Calendar.MILLISECOND, 0);

			alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
					cal.getTimeInMillis(), 3600000 * 7, getPendingIntent(0, "RINGER_OFF"));
  }
 }
}

When this triggers, message is empty:

public class ActionReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
    	 Bundle extras = intent.getExtras();
         String message = extras.getString("EXTRA"); //empty        
         if(message == "RINGER_OFF")
         {
	         AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
	         am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
         }
         else if(message == "RINGER_ON")
         {
        	 AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
	         am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
         }
    }
}

Android Solutions


Solution 1 - Android

UPDATE: Please see Vincent Hiribarren's solution


Old Answer... Haresh's code is not the complete answer... I used a Bundle and I tried without Bundle but I got null's either way when I attempting to obtain the strings from the extra's !!

The exact problem, in your code, is with the PendingIntent !

This is wrong if you're trying to pass extra's :

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, uniqueRequestCode, intent, 0);

Because the 0 for the flags is what will cause you a headache

This is the right way to do it - specify a flag !

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, uniqueRequestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);

This is probably such a popular problem because Google's sample code neglected to include Extra's in an Alarm.

Solution 2 - Android

I have some precisions that could help others, to be associated with the solution from Someone Somewhere. If you pass custom Parcelable objects as an extra, the operating system may not be able to process them, so an internal exception occurs and your extras are lost.

With Android N, even with PendingIntent.FLAG_UPDATE_CURRENT I cannot retrieve my custom Pacelable extras.

So I had to use system known Parcelable (like ParcelUuid) to reference some objects in a custom database instead of providing my whole Parcelable object.

Another solution is to convert Parcelable to a byte array that is correctly recognized by the system: https://stackoverflow.com/questions/18000093/how-to-marshall-and-unmarshall-a-parcelable-to-a-byte-array-with-help-of-parcel/18000094#18000094

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionthat_guyView Question on Stackoverflow
Solution 1 - AndroidSomeone SomewhereView Answer on Stackoverflow
Solution 2 - AndroidVincent HiribarrenView Answer on Stackoverflow