gpt4 book ai didi

android - 来自服务的 Marshmallow : Can't execute Settings. System.canWrite(Context)

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:03:39 24 4
gpt4 key购买 nike

我有一个在后台运行的 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 是一项服务,因为从 Activity 调用时不会发生这种情况。

最佳答案

这是一个“错误”,尽管如您所说,不应从 NotificationListenerService 调用此方法。您应该从普通的 ActivtyService 调用它。我稍后会解释原因。

根据你的踪迹,情况如下。

android.app.AppOpsManager.checkOpNoThrow(AppOpsManager.java:1536)

  /**
* Like {@link #checkOp} but instead of throwing a {@link SecurityException} it
* returns {@link #MODE_ERRORED}.
* @hide
*/
public int checkOpNoThrow(int op, int uid, String packageName) {
try {
return mService.checkOperation(op, uid, packageName); //1536
} catch (RemoteException e) {
}
return MODE_ERRORED;
}

虽然在 Parcel ( android.os.Parcel.readException(Parcel.java:1599) ) 中它正在 try catch RemoteException:

public final void readException(int code, String msg) {
switch (code) {
case EX_SECURITY:
throw new SecurityException(msg); // 1599
case EX_BAD_PARCELABLE:
throw new BadParcelableException(msg);
case EX_ILLEGAL_ARGUMENT:
throw new IllegalArgumentException(msg);
case EX_NULL_POINTER:
throw new NullPointerException(msg);
case EX_ILLEGAL_STATE:
throw new IllegalStateException(msg);
case EX_NETWORK_MAIN_THREAD:
throw new NetworkOnMainThreadException();
case EX_UNSUPPORTED_OPERATION:
throw new UnsupportedOperationException(msg);
}
throw new RuntimeException("Unknown exception code: " + code
+ " msg " + msg);
}

它抛出一个 SecurityException

作为SecurityException不是 RemoteException try-catch 无法捕获 checkOpNoThrow 中的异常,您会收到错误。

此外,如果您尝试使用应用程序的上下文(例如 this.getApplicationContext())而不是监听器服务的上下文,它也会导致错误。这是由于 canWrite 检查调用者的 UID。通知监听器具有与应用程序其他部分不同的 UID。

public static boolean canWrite(Context context) {
int uid = Binder.getCallingUid();
return isCallingPackageAllowedToWriteSettings(context, uid, getPackageNameForUid(
context, uid), false);
}

编辑

此错误在 AOSP issue tracker 上被跟踪.

关于android - 来自服务的 Marshmallow : Can't execute Settings. System.canWrite(Context),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34073645/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com