我可以在 android +24 上使用 checkPermission 而不是 checkSelfPermission 吗?

Can I use checkPermission instead of checkSelfPermission on android +24?

根据this document

to check if you have a permission, call the ContextCompat.checkSelfPermission()

并且 this document 说:

[Context].checkPermission() is to determine whether the given permission is allowed for a particular process and user ID running in the system

我在 android 24 上有以下代码:

smsPerm = "android.permission.SEND_SMS";
int result = checkPermission(smsPerm, Process.myPid(), Process.myUid());

result 将是:

话虽如此,似乎行为与 android +24 上的 checkSelfPermission 相同。我可以使用 checkPermission 而不是 checkSelfPermission 吗?

我不确定您认为自己获得了什么...但是,是的,您可以使用 Context#checkPermission() 而不是 ContextCompat.checkSelfPermission()

实现ContextCompat.checkSelfPermission()使用Context#checkPermission(),至少at the time that I posted this answer.

public static int checkSelfPermission(@NonNull Context context, @NonNull String permission) {
    if (permission == null) {
        throw new IllegalArgumentException("permission is null");
    }

    return context.checkPermission(permission, android.os.Process.myPid(), Process.myUid());
}

从长远来看,使用ContextCompat.checkSelfPermission()会更安全。可以对其进行更新以反映 Android.

新版本中应用的新规则