新权限的模型和RemoteViewsFactory

时间:2015-10-05 18:37:40

标签: android android-permissions android-6.0-marshmallow remoteview appcompat-v7-r23

我正在尝试使用新的Android 6的权限模型

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

但是我得到了例外

  

java.lang.SecurityException:uid 10153没有android.permission.UPDATE_APP_OPS_STATS

当我调用

时,在我的RemoteViewsFactory的onDataSetChanged()

Settings.canDrawOverlays(mContext))

记录:

  

java.lang.SecurityException:uid 10153没有   android.permission.UPDATE_APP_OPS_STATS。               在android.os.Parcel.readException(Parcel.java:1599)               在android.os.Parcel.readException(Parcel.java:1552)               at com.android.internal.app.IAppOpsService $ Stub $ Proxy.checkOperation(IAppOpsService.java:327)               在android.app.AppOpsManager.checkOpNoThrow(AppOpsManager.java:1523)               在android.provider.Settings.isCallingPackageAllowedToPerformAppOpsProtectedOperation(Settings.java:8425)               在android.provider.Settings.isCallingPackageAllowedToDrawOverlays(Settings.java:8385)               在android.provider.Settings.canDrawOverlays(Settings.java:1432)

有人可以向我解释这个例外吗?

更新

暂时解决了从RemoteViewsFactory的canDrawOverlays()内部开始的服务调用onDataSetChanged()

1 个答案:

答案 0 :(得分:0)

Settings.canDrawOverlays AppOpsManager.checkOpNoThrow Setting.System.canWrite 会在各种情况下导致此类崩溃(可能仅在Android 6.x),如下所示:

我发现的解决方法是使用处理程序在UI线程上运行它们,并等待您正在处理的线程上的结果。这是扩展NotificationListenerService的示例:

public void onNotificationPosted(StatusBarNotification sbn) {
   final Handler handler = new Handler(Looper.getMainLooper());
    handler.post(new Runnable() {
        @Override
        public void run() {
            //do the check of Settings.canDrawOverlays here.
            // notify that the result of the check was done
        }
    });
    //wait for result of the handler