NotificationListenerService 间歇性地为奥利奥工作

NotificationListenerService works intermittently for Oreo

我有一个class

public class NotifClass extends  NotificationListenerService{
@Override
    public IBinder onBind(Intent intent) {
        return super.onBind(intent);
    }

@Override
    public void onNotificationPosted(StatusBarNotification sbn){
String notifiTitle = getActiveNotifications()[i].getNotification().extras.getString("android.title");
}
}

我在

下了一个断点
String notificationTitle = getActiveNotifications()[i].getNotification().extras.getString("android.title");

在 onNotificationPosted(StatusBarNotification sbn) 方法中。

这是一个回调方法,当设备收到任何通知时调用。 在 运行 超过 Android 牛轧糖时,它工作得很好。 但它在 Android Oreo 中间歇性地出现。有时打有时不打。

我不确定真正的问题及其解决方案,但我是否可以使用 JobScheduler 读取 activeNotifications,因为我正面临服务问题? 我 运行 超过 API 26

我已经研究这个问题很长时间了。当我 运行 通过 Google Nexus/pixel 设备或​​模拟器时,此服务会间歇性工作。 我在 Honor 和 Samsung S9 上试过 运行 它,NotificationListenerService 在这方面工作得很好。 所以我得出结论,问题出在 Google 设备而不是服务。

请注意,如果设备 运行 在低 RAM 上,很有可能 NotificationListenerService 将停止工作。