如何在 Android 中更新服务中的 Bundle 数据?
我将制作一个简单的闹钟,我有用户界面,并且我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了类似的问题,经过在网上进行了大量研究后,我知道这个问题的解决方案隐藏在我们如何 PendingIntent 实例上。首先我创建这样的 PI 实例:-
但是如果你想更新 PendingIntent 实例因此,捆绑数据必须将
PendingIntent.FLAG_CANCEL_CURRENT
或FLAG_UPDATE_CURRENT
作为最后一个参数。 PendingIndent 实例将是:-或
这解决了我的问题,我希望它也能解决您的问题。
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:-
but if you want to update the instance of PendingIntent and so Bundle data you have to put
PendingIntent.FLAG_CANCEL_CURRENT
orFLAG_UPDATE_CURRENT
as last parameter.so the PendingIndent instance will be:-or
This solved my problem I hope it'll solve your's too.
每当我完成此操作时,我都会在发送端创建一个新的捆绑包并将字符串放入其中。
发送端的 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!