Android getActiveNotifications抛出SecurityException

Android getActiveNotifications抛出SecurityException,第1张

概述我有一个监听通知的NotificationService类.但是,当我调用getActiveNotifications()时,它会抛出SecurityException.是的,我在调用此方法之前已经检查了许可. 我在NotificationService中使用AsyncTask来获取通知.代码如下. private class AsyncProcessNotification extends As 我有一个监听通知的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,“获取”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.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.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.call(AsyncTask.java:295)       at java.util.concurrent.FutureTask.run(FutureTask.java:237)       at androID.os.AsyncTask$SerialExecutor.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)

我知道我必须做一些愚蠢但却无法弄清楚是什么.有人可以帮忙吗?谢谢!

解决方法

I hit the same error in my notification Listener service. I was able
to solve it by waiting for the Listener service to get connected to the
notification manager before calling any of the
NotificationListenerService methods.

@OverrIDepublic voID onListenerConnected() {    Log.i(TAG,"Listener connected");    ListenerConnected = true;}private class AsyncProcessNotification extends AsyncTask<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}
总结

以上是内存溢出为你收集整理的Android getActiveNotifications抛出SecurityException全部内容,希望文章能够帮你解决Android getActiveNotifications抛出SecurityException所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1124019.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存