【问题标题】:Can't put extras for an intent in notification不能在通知中为意图添加额外内容
【发布时间】:2011-08-17 20:17:06
【问题描述】:

我正在从具有额外整数意图的服务创建通知。出于某种原因,这并不像看起来那么容易。这就是我正在做的:

Notification notification = new Notification(R.drawable.notification_icon, "Title", 0);
Intent relaunchIntent = new Intent(this, SomeClass.class);
relaunchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
relaunchIntent.putExtra("load", getId());
notification.setLatestEventInfo(this, "Title", "Tap here to open", PendingIntent.getActivity(this, 0, relaunchIntent, 0);
notificationManager.notify(1234, notification);

然后从 SomeClass 中,它看起来像这样......

if(getIntent().getExtras() != null)
    load(getIntent().getExtras().getInt("load", -1));

奇怪的是,当我从 getInt() 方法中读取值时,我得到的结果不一致。有时我会得到我在之前的通知中设置的东西的值。例如,当我将额外设置为 4 时,我得到的值为 3。我知道这非常令人困惑和奇怪,这就是为什么我想知道是否有人经历过这样的事情以及您可能采取了什么措施来解决它。谢谢!

【问题讨论】:

  • 可能想看看使用PendingIntent.FLAG_UPDATE_CURRENT 来自this answer 的评论。
  • 谢谢,这个问题的答案成功了。你应该考虑把它作为答案,我会接受的。

标签: android service notifications android-intent extra


【解决方案1】:

当您调用PendingIntent.getActivity 时,您应该尝试使用PendingIntent.FLAG_UPDATE_CURRENT。来自文档:

如果您创建的意图只有附加功能,则可以使用此选项 改变,不要在意任何收到你以前的实体 PendingIntent 将能够使用您的新附加功能启动它,即使 它们没有被明确地赋予它。

我自己遇到了这个问题,从this answer的评论中找到了答案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多