【问题标题】:Android Notification Intent.putExtraAndroid Notification Intent.putExtra
【发布时间】:2012-02-18 16:35:25
【问题描述】:

我有一个通知,当按下时应该启动一个活动,然后启动一个对话框。一切正常,但对话框从 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");
}

问题在于,无论用户选择什么通知,这些总是来自第一个通知的“文本”和“NotifyID”。

【问题讨论】:

    标签: android android-intent notifications


    【解决方案1】:

    使用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");
    }
    

    【讨论】:

    猜你喜欢
    • 2011-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多