gpt4 book ai didi

android - 使用 notificationManager.getActiveNotifications() 获取状态栏通知

转载 作者:行者123 更新时间:2023-11-30 05:07:24 24 4
gpt4 key购买 nike

我正在使用 notificationManager.getActiveNotifications() 获取所有状态栏通知,但它只返回应用程序锁的通知。每当在 whats 应用程序或任何其他应用程序中发生通知时,此功能不会给我其他应用程序的主动通知。 App-lock的通知有什么问题吗?因为它一直在运行。还是 getActiveNotifications() 只给我通知仅在前台的应用程序?

这是我获取状态栏通知的代码:

 NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
ArrayList<String> nPakageNameList = new ArrayList<>();
if (notificationManager != null) {
StatusBarNotification[] activeNotifications
= notificationManager.getActiveNotifications();
for (StatusBarNotification sbn : activeNotifications) {
nPakageNameList.add(sbn.getPackageName());
Log.i("SPakagesInN>>>", sbn.getPackageName());
}
}

它只是返回 AppLock 的包名。不提供什么应用程序或任何其他应用程序通知。有没有其他方法可以在状态栏中查看应用程序通知?

最佳答案

您可以使用 NotificationListenerService 而不是 NotificationManager。您可以在发布通知时处理 onNotificationPosted(StatusBarNotification sbn) 方法上的通知。此外,您还可以通过该服务的 getActiveNotifications() 方法获取 Activity 通知列表。

https://developer.android.com/reference/android/service/notification/NotificationListenerService

关于android - 使用 notificationManager.getActiveNotifications() 获取状态栏通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54340459/

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