安卓通知 Intent.putExtra

标签 android android-intent notifications

我有一个通知,当按下时应该启动一个 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/

相关文章:

java - RadioButton.setChecked() 似乎不起作用

android - 如何为在 ListView 中动态添加的项目创建 Activity ?

android - 在 Android 上通过 Intent 启动华为花瓣 map 方向

android - 当应用程序不在后台或前台时,无法将通知推送到通知管理器

iphone - 委托(delegate)VS。 iPhoneOS 中的通知

android - 与外部可触摸的对话

android - 将 cURL 转换为 Retrofit

ios - 使用 Firebase 的本地通知

android - 如何实时将消息从一项 Activity 发送到另一项 Activity

android - 将服务中的数据发送回我的 Activity