【发布时间】:2018-01-07 18:17:39
【问题描述】:
我有一个 NotificationService 类来监听通知。但是,当我调用 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)
我知道我一定是在做一些愚蠢的事情,但不知道是什么。有人可以帮忙吗?谢谢!
【问题讨论】:
-
你能发布堆栈跟踪吗?
-
@Juan Trace 添加了。
-
根据文档 getActiveNotifications() 必须在 onListenerConnected() 之后和 onListenerDisconnected() 之前调用。当您在 AsyncTask 中运行时,您是否正在处理这个问题,例如在断开连接时取消任务?
-
@Juan 感谢您的信息。我将更新代码,看看我是否再次遇到这些异常。套件。
-
有什么解决办法吗?
标签: android android-asynctask android-notifications android-securityexception notification-listener