gpt4 book ai didi

android - 单击通知不会启动 Activity

转载 作者:搜寻专家 更新时间:2023-11-01 07:59:44 30 4
gpt4 key购买 nike

我正在创建来自服务的通知;显示了通知,但是当我点击它时,没有任何反应:它应该打开一个 Activity 。

我的代码:

NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, "test", when);
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent = PendingIntent.getActivity(this, 0,
notificationIntent,PendingIntent.FLAG_CANCEL_CURRENT);

notification.setLatestEventInfo(this, "title", "message", intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);

但是,如果我在 Activity 中使用几乎相同的代码,我可以单击通知,然后显示我的 Activity 。我做错了什么?

编辑:事实证明,这段代码没有任何问题,存在一个不同的问题:当我的服务完成时,它使用上面的代码创建了通知。但是,该服务还广播它已完成,并且接收者创建了另一个通知,它使用不同的代码来创建通知(没有 PendingIntents,因此单击通知时没有定义的操作),并且该通知必须自己放置而不是原来的,正确的。

最佳答案

除了在 Android 3.0 以上使用 Notification.Builder 或支持库 v4 中的 NotificationCompat.Builder(正如@Raghunandan 在评论中所建议的那样)之外,我还遇到了同样的问题,可能有一个可能的通用解决方案来解决你的问题。

这是特定于 4.4 的,如下所示:Issue 63236:Notification with TaskStackBuilder.getPendingIntent() is not open the Activity在这里 Issue 61850: KitKat notification action Pending Intent fails after application re-install

一个已确认的解决方案是对与您将要创建的相同的 PendingIntent 执行 cancel() 操作。

对我有用的是修改目标 Activity 的 list 定义并添加android:exported="true" 在目标 Activity 的“Activity ”标签内。我假设您的情况就是 MainActivity。

例子:

<activity
android:name="com.your.MainActivity"
android:exported="true" >
.
.
</activity>

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

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