我有一个在后台运行的NotificationListener服务,当我执行Settings.System.canWrite(Settings.java:3742)时会抛出异常
12-03 18:25:33.490 2754-2771/? W/System.err? java.lang.SecurityException: uid 10057 does not have android.permission.UPDATE_APP_OPS_STATS. 12-03 18:25:33.490 2754-2771/? W/System.err? at android.os.Parcel.readException(Parcel.java:1599) 12-03 18:25:33.490 2754-2771/? W/System.err? at android.os.Parcel.readException(Parcel.java:1552) 12-03 18:25:33.490 2754-2771/? W/System.err? at com.android.internal.app.IAppOpsService$Stub$Proxy.checkOperation(IAppOpsService.java:327) 12-03 18:25:33.490 2754-2771/? W/System.err? at android.app.AppOpsManager.checkOpNoThrow(AppOpsManager.java:1536) 12-03 18:25:33.490 2754-2771/? W/System.err? at android.provider.Settings.isCallingPackageAllowedToPerformAppOpsProtectedOperation(Settings.java:8425) 12-03 18:25:33.490 2754-2771/? W/System.err? at android.provider.Settings.isCallingPackageAllowedToWriteSettings(Settings.java:8320) 12-03 18:25:33.490 2754-2771/? W/System.err? at android.provider.Settings$System.canWrite(Settings.java:3742)
这是因为提供的Context是一种服务,因为从活动调用时不会发生这种情况.