views:

326

answers:

2

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?

+1  A: 

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().

CommonsWare
Does this mean that even though the intent occurred in the past the system still knows about it?
That seems to work as long as the phone is not shut down and restarted. If that happens and then the intent is called again using FLAG_UPDATE_CURRENT then the event is not scheduled. Is there a way to check whether or not the intent exist in its current state?
"Does this mean that even though the intent occurred in the past the system still knows about it?" Yes, unless you use `FLAG_ONE_SHOT`. "Is there a way to check whether or not the intent exist in its current state?" No, but try `FLAG_CANCEL_CURRENT` or `FLAG_ONE_SHOT` instead, then, depending on how you are trying to use the `PendingIntents`.
CommonsWare
I think FLAG_CANCEL_CURRENT and then calling again is a good choice. Thanks for your help. I have really enjoyed your books as well.
Glad to hear it! Stay tuned, as there will be a massive round of updates to them all in the next 5-7 weeks, once Google ships FroYo.
CommonsWare
A: 

Multiple PendingIntents:

Intent notificationIntent = new Intent(this, Oconf.class);
notificationIntent.setData(Uri.parse("text"));

Then, onNewIntent(Intent intent):

String text = intent.getData().toString();
embo