来自通知的Android刷新活动

Lar*_*ark 4 android android-intent android-activity

我有一个程序,我在其中调用通知.通知,如果您将其下拉,则启动新活动.

mNotificationManager = (NotificationManager) getSystemService(ns);

int icon = R.drawable.stat_sys_secure_green;
CharSequence tickerText = "Browser Security Enabled";
long when = System.currentTimeMillis();

notification = new Notification(icon, tickerText, when);

Context context = getApplicationContext();
CharSequence contentTitle = "Browser Security";
CharSequence contentText = "Security Vulnerability Detected";
Intent notificationIntent = new Intent(this, PrivacyMessage.class);

//Test Extra
notificationIntent.putExtra("Primary Key", "Primary Text");

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

mNotificationManager.notify(HELLO_ID, notification);
Run Code Online (Sandbox Code Playgroud)

当我想要刷新辅助活动时,问题会出现在代码中.主要活动应该能够动态地更改其中的额外内容.我尝试通过启动新意图来做到这一点.

CharSequence contentTitle = "Browser Security";
CharSequence contentText = "Test New Notification";
Intent intent = new Intent(this, PrivacyMessage.class);
notification.icon = R.drawable.stat_sys_secure_orange;

intent.putExtra("Test Thing", "Test Value");
//intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

PendingIntent cI = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(getApplicationContext(), "New Title", "NewText", cI);
mNotificationManager.notify(HELLO_ID, notification);
Run Code Online (Sandbox Code Playgroud)

现在,当我执行该代码时,弹出新的通知标题,图标颜色发生变化,下拉菜单反映新标题和附加信息.但是,当我单击它时,它不会使用新意图启动活动.相反,它只是用旧的附加功能拉出旧的活动.我尝试了FLAG_ACTIVITY_CLEAR_TOP和FLAG_ACTIVITY_NEW_TASK,但似乎都没有人清除旧的辅助活动并创建一个新活动.关于我如何做到这一点的任何想法?

Lar*_*ark 10

显然这实际上是android环境的一个bug /功能.除非使用唯一的requestCode传递pendingIntent(),否则它只会检索最初使用该数字传递的旧intent.

可在此处找到详细信息:http: //groups.google.com/group/android-developers/browse_thread/thread/ad855bb57042c2bd/e84c8d6fececf6e4?lnk=gst&q=notification#e84c8d6fececf6e4

他们提出的解决方案是每次pendingIntent.getActivity(Context context, int requestCode, Intent intent, int flags)调用时简单地增加requestCode ,并按照我最初使用它的方式设置标志

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Run Code Online (Sandbox Code Playgroud)

哪个,似乎不是一个完美的解决方案,但它的工作原理.谢谢你们的帮助!

  • 或者更好地将PendingIntent.FLAG_CANCEL_CURRENT标志与PendingIntent.getActivity()一起使用 (3认同)