53

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);
         }
    }
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
that_guy
  • 2,313
  • 4
  • 33
  • 46

2 Answers2

85

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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Someone Somewhere
  • 23,475
  • 11
  • 118
  • 166
  • I thought I was going insane until I found this. I was passing 3 extras and was only ever receiving 1 of the extras. Adding a flag instead of a 0 resolved my issue. +100000000 – hooked82 Jun 10 '16 at 21:32
  • 1
    @NeonWarge as of Android "N", Google made the API even more "interesting" (I wish Google would have fixed the API). You now have to convert your data to a byte array. Please see Vincent Hiribarren's solution. https://stackoverflow.com/a/41429570/550471 – Someone Somewhere Jul 11 '17 at 19:42
44

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: How to marshall and unmarshall a Parcelable to a byte array with help of Parcel?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Vincent Hiribarren
  • 5,254
  • 2
  • 41
  • 65
  • 7
    All answers with flags are insufficient. **This** ^ is the correct one. Thank you. – Miha_x64 Feb 28 '17 at 13:47
  • Than how to send the byte array to the BroadcastReceiver ? – aleksandrbel Apr 12 '17 at 07:04
  • @aleksandrbel By adding it as an extra to your Intent. For instance you have the method `putExtra (String name, byte[] value)` on `Intent`. Then in your `BroadcastReceiver` you extract the byte array from the Intent using `getByteArrayExtra`. The intent is provided to your receiver via the `onReceive` method. – Vincent Hiribarren Apr 12 '17 at 08:13
  • This should be the right answer for everyone as it will work on all versions Thank you !! – thunder413 May 20 '17 at 02:37
  • 3
    see https://stackoverflow.com/a/40515978/696517 that give more detail on this same problematic – jrmgx May 29 '17 at 11:50
  • Interesting how this problem has evolved - hopefully this works on Android "O" :-D Thanks for posting the updated solution – Someone Somewhere Jun 08 '17 at 15:21
  • @SomeoneSomewhere this works on Android 26 Oreo as of today. Thanks though. – ralphgabb Oct 12 '18 at 04:27
  • Serializable are also removed by the OS :( – Eric Sep 27 '22 at 13:36