如何在 Android 中更新服务中的 Bundle 数据?

发布于 2024-12-28 09:25:26 字数 594 浏览 0 评论 0原文

我将制作一个简单的闹钟,我有用户界面,并且我使用 Bundle 发送闹钟的用户配置(如音量值或音调类型)。 在主 Activity 中,我有:

Bundle b = new Bundle();
b.putString("tone", toneS.getSelectedItem().toString()); 

我将其发送到 BroadcastReceiver:

Intent intent = new Intent(SetAlarm.this, MessageReceiver.class);
            intent.putExtras(setBoundle());

我以这种方式接收 BroadcastReceiver 中的 Bundle:

Bundle b2 = new Bundle();
        b2 = intent.getExtras();

它第一次工作完美,但之后虽然主 Activity 中的 Bundle 有来自 UI 的新数据,但 BroadcastReceiver 只是保留旧数据。

谁能解释一下这个问题吗?

I am going to make a simple Alarm Clock, I have user interface and I am using Bundle to send user configurations(Like volume value or tone type) for Alarm.
In main Activity I have:

Bundle b = new Bundle();
b.putString("tone", toneS.getSelectedItem().toString()); 

And I send it to BroadcastReceiver:

Intent intent = new Intent(SetAlarm.this, MessageReceiver.class);
            intent.putExtras(setBoundle());

And I receive the Bundle in BroadcastReceiver in this way:

Bundle b2 = new Bundle();
        b2 = intent.getExtras();

It works perfectly for the first time but after it although Bundle in the main activity has new data from UI but BroadcastReceiver just keep the old data.

Can anyone explain the issue?

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

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

发布评论

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

评论(2

俏︾媚 2025-01-04 09:25:26

我遇到了类似的问题,经过在网上进行了大量研究后,我知道这个问题的解决方案隐藏在我们如何 PendingIntent 实例上。首先我创建这样的 PI 实例:-

 pendingIntent = PendingIntent.getService(SettingsScreen.this, 0, myIntent, 0);

但是如果你想更新 PendingIntent 实例因此,捆绑数据必须将 PendingIntent.FLAG_CANCEL_CURRENTFLAG_UPDATE_CURRENT 作为最后一个参数。 PendingIndent 实例将是:-

 pendingIntent = PendingIntent.getService(SettingsScreen.this, 0, myIntent, 
 PendingIntent.FLAG_CANCEL_CURRENT);

pendingIntent = PendingIntent.getService(SettingsScreen.this, 0, myIntent, 
PendingIntent.FLAG_UPDATE_CURRENT);

这解决了我的问题,我希望它也能解决您的问题。

I was facing similar problem and after a lot of research on net I got to know that the solution of this problem is hide on how we PandingIntent instance.Firstly i create PI instance like this:-

 pendingIntent = PendingIntent.getService(SettingsScreen.this, 0, myIntent, 0);

but if you want to update the instance of PendingIntent and so Bundle data you have to put PendingIntent.FLAG_CANCEL_CURRENT or FLAG_UPDATE_CURRENT as last parameter.so the PendingIndent instance will be:-

 pendingIntent = PendingIntent.getService(SettingsScreen.this, 0, myIntent, 
 PendingIntent.FLAG_CANCEL_CURRENT);

or

pendingIntent = PendingIntent.getService(SettingsScreen.this, 0, myIntent, 
PendingIntent.FLAG_UPDATE_CURRENT);

This solved my problem I hope it'll solve your's too.

苦笑流年记忆 2025-01-04 09:25:26

每当我完成此操作时,我都会在发送端创建一个新的捆绑包并将字符串放入其中。
发送端的 Intent 可以根据需要重复使用

希望这会有所帮助!

Whenever I've done this I make a new Bundle in the sending end and put the string in that.
The Intent on the sending end can be reused as much as you like

Hope this helps!

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