Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Android getActiveNotifications抛出SecurityException大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个监听通知的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$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> {
    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所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:android抛出