Intent.putExtras not consistent
-
27-09-2019 - |
Question
I have a weird situation with AlarmManager. I am scheduling an event with AlarmManager and passing in a string using intent.putExtra. The string is either silent or vibrate and when the receiver fires the phone should either turn of the ringer or set the phone to vibrate. The log statement correctly outputs the expected value each time.
Intent intent;
if (eventType.equals("start")) {
intent = new Intent(context, SReceiver.class);
} else {
intent = new Intent(context, EReceiver.class);
}
intent.setAction(eventType+Long.toString(newId));
Log.v("EditQT",ringerModeType.toUpperCase());
intent.putExtra("ringerModeType", ringerModeType.toUpperCase());
PendingIntent appIntent = PendingIntent.getBroadcast(context, 0,
intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService (Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
appIntent);
The receiver that fires when the alarm executes also has a log statement and I can see the first time around that the statement outputs the expected string either SILENT or VIBRATE but for each subsequent execution the output shows the original value on the receiver end. The alarm executes and then I change the value for putExtra to opposite string and the receiver still displays the previous value event though the call from the code above shows that the new value was passed in. The value for setAction is the same each time.
audioManager = (AudioManager) context.getSystemService(Activity.AUDIO_SERVICE);
Log.v("Start",intent.getExtras().get("ringerModeType").toString());
if (intent.getExtras().get("ringerModeType").equals("SILENTMODE")) {
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
} else {
audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
}
Any thoughts?
Solution
Your very question was asked six hours ago.
If you will have multiple PendingIntents
at the same time with different extras, you will need to vary something else in the Intents
, like the action string or the Uri
, as described in the linked-to issue above.
If you will only have one PendingIntent
at a time, but your extra may vary, just use FLAG_UPDATE_CURRENT
in your call to getBroadcast()
.
OTHER TIPS
Multiple PendingIntents:
Intent notificationIntent = new Intent(this, Oconf.class);
notificationIntent.setData(Uri.parse("text"));
Then, onNewIntent(Intent intent):
String text = intent.getData().toString();