我有一个通知,当按下时应该启动一个 Activity ,该 Activity 又会启动一个对话框。这一切都有效,但对话框从通知中提取信息认为 Intent.putExtra()。问题是,它总是从 putExtra 中提取最新信息,因此如果用户单击更新的通知,他们会从旧的通知中获取信息。有没有办法指定哪个 putExtra 与哪个通知一起使用?
这是我使用的代码: ID 是一个 int,UserText 是一个字符串:
Intent notificationIntent = new Intent(this, DialogActivity.class);
notificationIntent.putExtra("Text", UserText).putExtra("NotifyID", ID);
在 DialogActivity 中
Bundle extras = getIntent().getExtras();
String test;
int NID;
if (extras != null) {
test = extras.getString("Text");
NID = extras.getInt("NotifyID");
}
问题是无论用户选择什么通知,它们始终是第一个通知中的“Text”和“NotifyID”。
最佳答案
使用removeExtra
。在你的情况下:
Bundle extras = getIntent().getExtras();
String test;
int NID;
if (extras != null) {
test = extras.getString("Text");
NID = extras.getInt("NotifyID");
getIntent().removeExtra("Text");
getIntent().removeExtra("NotifyID");
}
关于安卓通知 Intent.putExtra,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9342957/