我打算制作一个简单的闹钟,我有用户界面,我正在使用Bundle为Alarm发送用户配置(如音量值或音调类型)。 在主要活动中我有:
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();
虽然主要活动中的Bundle具有来自UI的新数据但是BroadcastReceiver只保留旧数据,所以它第一次完美地工作但是在它之后。
任何人都可以解释这个问题吗?
答案 0 :(得分:4)
我遇到了类似的问题,经过对网络的大量研究后我才知道这个问题的解决方案是隐藏在我们PandingIntent实例的方式上。首先我创建这样的PI实例: -
pendingIntent = PendingIntent.getService(SettingsScreen.this, 0, myIntent, 0);
但是如果你想更新PendingIntent的实例以及Bundle数据,你必须将PendingIntent.FLAG_CANCEL_CURRENT
或FLAG_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);
这解决了我的问题,我希望它也能解决你的问题。
答案 1 :(得分:2)
每当我完成此操作时,我会在发送端创建一个新的Bundle并将字符串放入其中。
发送端的Intent可以根据需要重复使用
希望这有帮助!