PendingIntent正常工作的第一通知,但不正确的休息 [英] PendingIntent works correctly for the first notification but incorrectly for the rest

查看:127
本文介绍了PendingIntent正常工作的第一通知,但不正确的休息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  protected void displayNotification(String response) {
    Intent intent = new Intent(context, testActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);

    Notification notification = new Notification(R.drawable.icon, "Upload Started", System.currentTimeMillis());
    notification.setLatestEventInfo(context, "Upload", response, pendingIntent);

    nManager.notify((int)System.currentTimeMillis(), notification);
}

这个函数会被多次调用。我想为每一个通知,推出testActivity点击时。遗憾的是,只有第一个通知启动testActivity。单击其余导致通知窗口最小化。

This function will be called multiple times. I would like for each notification to launch testActivity when clicked. Unfortunately, only the first notification launches testActivity. Clicking on the rest cause the notification window to minimize.

额外信息:功能displayNotification()是一类被称为UploadManager。上下文是从实例活动传递到UploadManager。功能displayNotification()是从一个函数调用多次,也在UploadManager,即在一个AsyncTask的运行

Extra information: Function displayNotification() is in a class called UploadManager. Context is passed into UploadManager from the activity that instantiates. Function displayNotification() is called multiple times from a function, also in UploadManager, that is running in an ASyncTask.

编辑1:我忘了提,我传递字符串响应到意向意图作为一个额外的

Edit 1: I forgot to mention that I am passing String response into Intent intent as an extra.

  protected void displayNotification(String response) {
    Intent intent = new Intent(context, testActivity.class);
    intent.putExtra("response", response);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

这使得一个很大的区别,因为我需要额外的反应,以反映在创建通知时什么字符串回应是。相反,使用PendingIntent.FLAG_UPDATE_CURRENT,额外的反应反映了字符串的反应是到displayNotification()的最后一次通话。

This makes a big difference because I need the extra "response" to reflect what String response was when the notification was created. Instead, using PendingIntent.FLAG_UPDATE_CURRENT, the extra "response" reflects what String response was on the last call to displayNotification().

我知道这是为什么,从阅读FLAG_UPDATE_CURRENT的文档。但是,我不知道如何解决它的那一刻。

I know why this is from reading the documentation on FLAG_UPDATE_CURRENT. However, I am not sure how to work around it at the moment.

推荐答案

不要使用 Intent.FLAG_ACTIVITY_NEW_TASK 的PendingIntent.getActivity,使用<一个href="http://developer.android.com/reference/android/app/PendingIntent.html#FLAG_ONE_SHOT">FLAG_ONE_SHOT而不是

Don't use Intent.FLAG_ACTIVITY_NEW_TASK for PendingIntent.getActivity, use FLAG_ONE_SHOT instead

这篇关于PendingIntent正常工作的第一通知,但不正确的休息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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