Intent.putExtras并不一致Intent、putExtras

2023-09-06 06:28:30 作者:城南花已开

我有AlarmManager一个奇怪的局面。我调度AlarmManager一个事件,并通过使用intent.putExtra的字符串。字符串为静音或振动模式,当接收器触发手机应该铃声任转弯或将手机设置为振动。日志报表输出正确的每一次的预期值。

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

这是火灾报警时也执行接收机有一个日志声明,我可以看到周围的首次声明输出预期的字符串,要么是静音或振动模式,但每个后续执行的输出显示,在接收端的原始值。报警执行,然后我对putExtra的值更改为另一串和接收器仍显示previous价值的事件,虽然上面从code来电显示,新价值在过去。对于setAction命令的值是同各一次。

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

有什么想法?

推荐答案

您非常有人问six小时前。

如果你有多个 PendingIntents 在同一时间用不同的演员,你将需要改变在意图,就像操作字符串或乌里,如描述的链接上面发布。

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.

如果你只会有一个的PendingIntent 的时间,但你的额外可能会有所不同,只要使用 FLAG_UPDATE_CURRENT 中您的来电 getBroadcast()

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