Intent.putExtras 不一致

发布于 2024-09-01 09:53:47 字数 1430 浏览 5 评论 0原文

我的 AlarmManager 遇到了一个奇怪的情况。我正在使用 AlarmManager 安排一个事件,并使用intent.putExtra 传入一个字符串。琴弦要么静音,要么振动,当接收器触发时,手机应该关闭铃声或将手机设置为振动。日志语句每次都正确输出期望值。

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

警报执行时触发的接收器也有一个日志语句,我可以看到该语句第一次输出预期的字符串 SILENT 或 VIBRATE,但对于每个后续执行,输出显示接收器端的原始值。警报执行,然后我将 putExtra 的值更改为相反的字符串,接收器仍然显示先前的值事件,尽管上面代码中的调用显示已传入新值。setAction 的值每次都是相同的。

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

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枯叶蝶 2024-09-08 09:53:47

六小时前提出了您的问题。

如果您同时有多个具有不同附加功能的 PendingIntents,则需要更改 Intents 中的其他内容,例如操作字符串或 Uri< /code>,如上面链接的问题中所述。

如果您一次只有一个 PendingIntent,但您的额外内容可能会有所不同,则只需在调用 getBroadcast() 时使用 FLAG_UPDATE_CURRENT 即可。

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

随心而道 2024-09-08 09:53:47

多个PendingIntents:

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

然后,onNewIntent(Intentintent):

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

Multiple PendingIntents:

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

Then, onNewIntent(Intent intent):

String text = intent.getData().toString();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文