本文介绍了从通知的Andr​​oid刷新活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,我打电话通知。该通知,如果你拉下来,推出了新的活动。

I have a program in which I call a notification. The notification, if you pull it down, launches a new 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);

问题是后话,在code,当我想刷新次要活动。主要活动应该是能够动态改变演员在里面。我试图通过推出新的意图这样做。

The problem comes later in the code, when I want to refresh the secondary activity. The main activity should be able to dynamically change the extras in it. I tried doing this by launching a new intent.

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);

现在,当我执行了code,新通知标题弹出,图标颜色的变化,和下拉反映新标题和附加信息。然而,当我点击它,它不会推出新的意图的活动。相反,它只是翻出老活动的老演员。我想这两个FLAG_ACTIVITY_CLEAR_TOP和FLAG_ACTIVITY_NEW_TASK,但没有一个似乎清除旧的次要活动,并创建一个新的。我怎么可能会做什么想法?

Now, when I execute that code, the new notification title pops up, the icon color changes, and the pulldown reflects the new title and addition information. However, when I click on it, it does not launch the activity with the new intent. Instead, it just pulls out the old activity with the old extras. I tried both FLAG_ACTIVITY_CLEAR_TOP, and FLAG_ACTIVITY_NEW_TASK, but neither one seems to clear the old secondary activity and create a new one. Any idea on how I might do that?

推荐答案

显然,这实际上是在Android环境的一个bug /功能。除非pendingIntent()传递了一个独特的请求,code,它只是检索最初通过与一些老的目的。

Apparently this is actually a bug/feature of the android environment. Unless a pendingIntent() is passed with a unique requestCode, it simply retrieves the old intent that was originally passed with that number.

详细信息可以在这里找到:<一href="http://groups.google.com/group/android-developers/browse_thread/thread/ad855bb57042c2bd/e84c8d6fececf6e4?lnk=gst&q=notification#e84c8d6fececf6e4">http://groups.google.com/group/android-developers/browse_thread/thread/ad855bb57042c2bd/e84c8d6fececf6e4?lnk=gst&q=notification#e84c8d6fececf6e4

Details can be found here:http://groups.google.com/group/android-developers/browse_thread/thread/ad855bb57042c2bd/e84c8d6fececf6e4?lnk=gst&q=notification#e84c8d6fececf6e4

他们想出解决的办法是简单的,每次递增请求code pendingIntent.getActivity(上下文的背景下,INT请求code,意图意图,诠释标志)被调用,并设置标志的方式,我曾与做它最初

The solution they came up with was to simply increment the requestCode every time pendingIntent.getActivity(Context context, int requestCode, Intent intent, int flags) is called, and set the flags the way I had done it originally with

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

其中,似乎并不像一个完美的解决方案,但它的工作原理。谢谢你们的帮助!

Which, doesn't seem like a perfect solution, but it works. Thank you guys for your help!

这篇关于从通知的Andr​​oid刷新活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 09:40