不能把临时演员在通知的意图 [英] Can't put extras for an intent in notification

查看:148
本文介绍了不能把临时演员在通知的意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm creating a notification from a Service that has an intent with an extra integer. For some reason, this isn't as easy as it seems. This is what I'm doing:

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的,它读取像这样...

And then from SomeClass, it reads like this...

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

什么是奇怪的是,我得到不一致的结果,当我读到从调用getInt()法的价值。有时我的东西,我在previous通知设置的值。例如,我得到的值为3时,我设置了额外的为4。我知道这是非常混乱和陌生,这是如果有人经历了这样的事,什么你可能已经做了修正,为什么我想知道它。谢谢!

What's weird is that I get inconsistent results when I read the value from the getInt() method. Sometimes I get values of things I set in a previous notifications. For example, I get a value of 3 when I had set that extra to be 4. I know this is very confusing and strange, which is why I'm wondering if anyone has experienced anything like this and what you may have done to fix it. Thanks!

推荐答案

您应该尝试使用的 PendingIntent.FLAG_UPDATE_CURRENT 当你调用 PendingIntent.getActivity 。从文档:

You should try using PendingIntent.FLAG_UPDATE_CURRENT when you call PendingIntent.getActivity. From the documentation:

这可如果您要创建意图,其中只有额外使用
  改变,不关心收到你的previous的任何实体
  的PendingIntent将能够与你的新演员,即使启动它
  他们没有明确给出吧。

This can be used if you are creating intents where only the extras change, and don't care that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly given to it.

我遇到了这个问题我自己,发现从<一个注释的答案href=\"http://stackoverflow.com/questions/1198558/how-to-send-parameters-from-a-notification-click-to-an-activity/1201239#1201239\">this回答。

I ran into this issue myself, and found the answer from a comment on this answer.

这篇关于不能把临时演员在通知的意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆