android挂起意图通知问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3009059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android pending intent notification problem
提问by spagi
I have a alarm thing going on in my app and it launches a notification that then when pressed launched an activity. The problem is that when I create more than one alarm then the activity launched from the notification gets the same extras as the first one. I think the problem is either with the intent i put in the pending intent or in the pending intent itself. I think I might need to put a flag on one of these but I dont know which one.
我的应用程序中有一个警报事件,它会启动一个通知,然后在按下时启动一个活动。问题是,当我创建多个警报时,从通知启动的活动将获得与第一个相同的附加功能。我认为问题要么出在我放入待处理意图中的意图,要么出在待处理意图本身中。我想我可能需要在其中一个上放一面旗帜,但我不知道是哪一个。
Intent showIntent =new Intent(context, notificationreceiver.class);
showIntent.putExtra("details", alarmname);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
showIntent, 0);
notification.setLatestEventInfo(context, "The event is imminent",
alarmname, contentIntent);
And the receiver of the notification
和通知的接收者
Bundle b = getIntent().getExtras();
String eventname = b.getString("details");
details.setText(eventname);
The "details" extra is the same to every the next time a notification happens instead of having the different value. Until I set the intents I am sure that the correct value goes to the "details" so its a problem of getting the first intent everytime i press any notification. How can I make it to launch the correct intents? Hope I was as clear as i could Thanks!
每次发生通知时,“详细信息”额外内容都相同,而不是具有不同的值。在我设置意图之前,我确信正确的值会出现在“详细信息”中,因此每次按下任何通知时都会出现第一个意图的问题。我怎样才能让它启动正确的意图?希望我尽可能清楚谢谢!
采纳答案by CommonsWare
The problem is that when I create more than one alarm then the activity launched from the notification gets the same extras as the first one.
问题是,当我创建多个警报时,从通知启动的活动将获得与第一个相同的附加功能。
Correct.
正确的。
How can I make it to launch the correct intents?
我怎样才能让它启动正确的意图?
That depends on whether you have two alarms that will be registered at once, or not.
这取决于您是否有两个将同时注册的警报。
If not, you can use FLAG_ONE_SHOT
or one of the other PendingIntent
flagsto have your second PendingIntent
use the newer extras.
如果没有,您可以使用FLAG_ONE_SHOT
或 其他PendingIntent
标志之一让您的第二个PendingIntent
使用较新的附加功能。
If, however, you will have two alarms registered at once, with different Intent
extras, you will need to make the two Intents
be more materially different, such that filterEquals()
returns false
when comparing the two. For example, you could call setData()
or setAction()
and provide different values for each Intent
.
但是,如果您将同时注册两个警报,并具有不同的Intent
附加功能,则需要使两者Intents
有更大的不同,以便在比较两者时filterEquals()
返回false
。例如,您可以调用setData()
或setAction()
并为每个 提供不同的值Intent
。
回答by u-ramos
The way I solved that problem was by assigning a unique requestCodewhen you get the PendingIntent:
我解决这个问题的方法是在获得 PendingIntent 时分配一个唯一的requestCode:
PendingIntent.getActivity(context, requestCode, showIntent, 0);
By doing so you are registering with the system different/unique intent instances. Tip:A good way of making the requestCode unique would be by passing to it the current system time.
通过这样做,您正在向系统注册不同/独特的意图实例。 提示:使 requestCode 唯一的一个好方法是将当前系统时间传递给它。
int requestID = (int) System.currentTimeMillis();
回答by bsh
I had this issue in my app and just generated a random number to over come overriding notifications intent:
我在我的应用程序中遇到了这个问题,只是生成了一个随机数来覆盖通知意图:
int random= new Random().nextInt();
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
random,
PendingIntent.FLAG_UPDATE_CURRENT
);
回答by Dam. Matics
I followed the solution provided by U-ramos and this worked for me
我遵循了 U-ramos 提供的解决方案,这对我有用
int requestID = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getActivity(this, requestID, showIntent, 0);
回答by Abeer Sul
another solution:
另一种解决方案:
use the PendingIntent.FLAG_UPDATE_CURRENTlike this:
像这样使用PendingIntent.FLAG_UPDATE_CURRENT:
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,i, PendingIntent.FLAG_UPDATE_CURRENT);
this worked for me
这对我有用