Android 从通知刷新 Activity

发布于 2024-11-26 14:11:45 字数 1721 浏览 0 评论 0原文

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

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

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

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

现在,当我执行该代码时,会弹出新的通知标题,图标颜色会发生变化,并且下拉菜单会反映新的标题和附加信息。但是,当我单击它时,它不会启动具有新意图的活动。相反,它只是将旧的活动与旧的额外内容一起拉出来。我尝试了 FLAG_ACTIVITY_CLEAR_TOP 和 FLAG_ACTIVITY_NEW_TASK,但似乎都没有清除旧的辅助活动并创建新的辅助活动。关于我如何做到这一点有什么想法吗?

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

前事休说 2024-12-03 14:11:45

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

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

他们提出的解决方案是每次只需增加 requestCode pendingIntent.getActivity(Context context, int requestCode, Intent Intent, int flags) 被调用,并按照我最初使用 Which 完成的方式设置标志

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

,这似乎不是一个完美的解决方案,但它有效。谢谢你们的帮助!

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.

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

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!

唯憾梦倾城 2024-12-03 14:11:45

我认为,首先,您应该忘记FLAG_ACTIVITY_NEW_TASK,因为这将打开一个新任务(活动组),而不会清除您之前打开的任何内容。 FLAG_ACTIVITY_CLEAR_TOP 对您也没有用,因为,如果我正确理解该场景,您的任务只有两个活动,而您的目标活动是次要的。

所以这是我的问题...如果第二段代码是在 Activity 上下文中执行的,为什么不直接使用新的额外内容调用 startActivity 呢?这将允许您处理辅助活动的 onStart 方法上的新附加功能。

问候。

I think, first of all, you should forget about the FLAG_ACTIVITY_NEW_TASK, cause this would open a new task (group of activities) without clearing anything you previously opened. The FLAG_ACTIVITY_CLEAR_TOP wouldn't vbe useful for you either, cause, if I understand the scenario correctly your taks has only two activities, and your target activity is the secondary.

So here it's my question... If the second piece of code is executed within an Activity context, why don't you just call startActivity with the new extras? This would allow you to handle the new extras on the onStart method of the secondary activity.

Regards.

分開簡單 2024-12-03 14:11:45

您是否重写 onNewIntent( )来捕获新的意图,或者只是调用 getIntent()? onNewIntent() 文档表示 getIntent() 将继续返回用于启动活动的原始意图,除非您从 onNewIntent() 调用 setIntent()。

Are you overriding onNewIntent() to catch the new intent, or just calling getIntent()? The onNewIntent() documentation says that getIntent() will continue to return the original intent used to launch the activity, unless you call setIntent() from onNewIntent().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文