By that_guy


2013-09-06 04:08:45 8 Comments

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);
         }
    }
}

2 comments

@Vincent Hiribarren 2017-01-02 15:38:32

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?

@Miha_x64 2017-02-28 13:47:10

All answers with flags are insufficient. This ^ is the correct one. Thank you.

@aleksandrbel 2017-04-12 07:04:11

Than how to send the byte array to the BroadcastReceiver ?

@Vincent Hiribarren 2017-04-12 08:13:36

@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.

@thunder413 2017-05-20 02:37:37

This should be the right answer for everyone as it will work on all versions Thank you !!

@jrmgx 2017-05-29 11:50:39

see stackoverflow.com/a/40515978/696517 that give more detail on this same problematic

@Someone Somewhere 2017-06-08 15:21:34

Interesting how this problem has evolved - hopefully this works on Android "O" :-D Thanks for posting the updated solution

@ralphgabb 2018-10-12 04:27:52

@SomeoneSomewhere this works on Android 26 Oreo as of today. Thanks though.

@Someone Somewhere 2015-01-28 22:25:40

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.

@hooked82 2016-06-10 21:32:45

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

@Neon Warge 2017-07-11 14:55:37

Jesus christ even this doesn't even work!

@Someone Somewhere 2017-07-11 19:42:21

@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. stackoverflow.com/a/41429570/550471

Related Questions

Sponsored Content

106 Answered Questions

[SOLVED] R cannot be resolved - Android error

6 Answered Questions

53 Answered Questions

35 Answered Questions

96 Answered Questions

[SOLVED] "cannot resolve symbol R" in Android Studio

3 Answered Questions

[SOLVED] How to fix Service Instance Issue?

17 Answered Questions

[SOLVED] How do I get extra data from intent on Android?

1 Answered Questions

5 Answered Questions

0 Answered Questions

Send SMS on Android

Sponsored Content