gpt4 book ai didi

android - 通知启动错误的 Activity

转载 作者:行者123 更新时间:2023-11-30 03:03:26 25 4
gpt4 key购买 nike

我的问题是:单击通知消息会启动正在运行的 Activity ,而不是预期的 Activity (我的代码中的 BatchActivity); (否则,如果 mainactivity 正在运行,它会返回到那个,否则 android 的主屏幕启动。它似乎只是消失状态屏幕)

它过去工作正常,直到我使用相同的通知 ID 将之前的通知打结到它,从那时起 BatchActivity 就没有启动。在 Debug模式下,永远不会到达 onCreate 中的第一行。

我在 Sony Xperia Android 设备 4.3 上测试了它,为目标 19 构建。我的代码:...

   if (qty>0) {
Intent intent = new Intent(context, BatchActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
notification = new NotificationCompat.Builder(context)
.setContentTitle(passing[0].get(6))
.setContentText("Subject")
.setTicker("hihi ho haha")
.setNumber(qty)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.setAutoCancel(true)
.addAction(R.drawable.ic_launcher, "And more", pIntent).build();
notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);
} else {
notificationManager.cancel(NOTIFICATION_ID);
}

...

通知正在更新以前的通知。

提前致谢!我卡住了

最佳答案

这是因为系统不会假设仅仅因为我们将一个新的 Intent 对象传递给待处理的 Intent ,我们希望传递这个新的 intent 对象并且只是保持旧的(初始的)待处理的 intent 处于 Activity 状态。

为了获得所需的语义,我们需要传递一个标志来告诉系统:

PendingIntent pintent = 
PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);

此标志 (FLAG_CANCEL_CURRENT) 告诉系统旧的待定 Intent 不再有效,它应该取消(=删除)它,然后为我们创建一个新的。还有更多可能的标志,在 PendingIntent 的 javadoc 页面上有描述。

解决方案 Heiko Rupp

关于android - 通知启动错误的 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22197220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com