Android getActiveNotifications throws SecurityException



我有一个侦听通知的通知服务类。但是,当我调用getActiveNotifications((时,它会抛出SecurityException。是的,在调用此方法之前,我已经检查了权限。

我在NotificationService中使用AsyncTask来获取通知。代码如下。

private class AsyncProcessNotification extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
int notificationType = MainApplication.settingNotificationType;
MainApplication.clearNotificationItems();
if (MainApplication.settingNotificationType == Constants.KEY_NOTIFICATION_TYPE_DISABLED) {
Log.i(TAG, "Notifications disabled");
return null;
}
if (PermissionHelper.isNotificationPermissionGranted(getApplicationContext())) {
if (getActiveNotifications() == null || getActiveNotifications().length == 0) {
Log.i(TAG, "No notifications found");
return null;
}
Log.i(TAG, "Getting " + getActiveNotifications().length +" notifications");
Log.i(TAG, "Notification type  " + notificationType);
for (StatusBarNotification statusBarNotification : getActiveNotifications()) {
// Process notifications
}
} else {
//
}
return null;
}
@Override
protected void onPostExecute(Void result) {
Intent notify = new Intent(Constants.ACTION_NOTIFICATION_LISTENER);
sendBroadcast(notify);
}
}

奇怪的事情是根据Crashlytics,有时在if (getActiveNotifications() == null || getActiveNotifications().length == 0)失败,有时在Log.i(TAG, "Getting " + getActiveNotifications().length +" notifications");

失败为了检查权限,我使用以下方法。

public static boolean isNotificationPermissionGranted(Context context) {
Set<String> appList = NotificationManagerCompat.getEnabledListenerPackages(context);
for (String l:appList) {
if (l.equals(context.getPackageName())) {
return true;
}
}
return false;
}

堆栈跟踪:

Fatal Exception: java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by java.lang.SecurityException: Disallowed call from unknown notification listener: android.service.notification.INotificationListener$Stub$Proxy@3e9880d
at android.os.Parcel.readException(Parcel.java:1599)
at android.os.Parcel.readException(Parcel.java:1552)
at android.app.INotificationManager$Stub$Proxy.getActiveNotificationsFromListener(INotificationManager.java:1046)
at android.service.notification.NotificationListenerService.getActiveNotifications(NotificationListenerService.java:467)
at android.service.notification.NotificationListenerService.getActiveNotifications(NotificationListenerService.java:420)
at com.afd.app.lockscreen.ios11.lib.service.NotificationService$AsyncProcessNotification.doInBackground(NotificationService.java:120)
at com.afd.app.lockscreen.ios11.lib.service.NotificationService$AsyncProcessNotification.doInBackground(NotificationService.java:97)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)

我知道我一定在做傻事,但不知道是什么。有人可以帮忙吗?谢谢!

我在通知侦听器服务中遇到了同样的错误。我能够 通过等待侦听器服务连接到 在调用任何通知侦听器服务方法。

@Override
public void onListenerConnected() {
Log.i(TAG, "Listener connected");
listenerConnected = true;
}
private class AsyncProcessNotification extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... params) {
...
// wait until our notification listener service is connected
// to the notification manager
Log.i(TAG, "Waiting for listener to be connected...");
while(!listenerConnected);
// Call getActiveNotifications after this
for (StatusBarNotification sbn : getActiveNotifications()) {
processNotifications(sbn);
}
}
// rest of the AsyncTask here
}

就像@niranjan-nagaraju writen一样,你需要在取消通知之前检查sIsConnected

https://android.googlesource.com/platform/frameworks/base/+/b408e8ecd22853faa70e97d0596aac9e7dcf5596/services/core/java/com/android/server/notification/NotificationListeners.java#175

相关内容

  • 没有找到相关文章

最新更新