contextCompat.checkSelfPermission()和activityCompat.requestPermissions()有什么区别?

6
if (ContextCompat.checkSelfPermission(thisActivity,
            Manifest.permission.READ_CONTACTS)
    != PackageManager.PERMISSION_GRANTED) {

// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
        Manifest.permission.READ_CONTACTS)) {

    // Show an explanation to the user *asynchronously* -- don't block
    // this thread waiting for the user's response! After the user
    // sees the explanation, try again to request the permission.

} else {

    // No explanation needed, we can request the permission.

    ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.READ_CONTACTS},
            MY_PERMISSIONS_REQUEST_READ_CONTACTS);

    // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
    // app-defined int constant. The callback method gets the
    // result of the request.
}

在这里,我使用“ContextCompat”检查权限是否已经被授予,并且我再次使用“ActvityCompat”请求权限......我需要知道这两种方法的区别是什么?

2个回答

8
文档中写道:

To check if you have a permission, call the ContextCompat.checkSelfPermission() method. For example, this snippet shows how to check if the activity has permission to write to the calendar:

int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
    Manifest.permission.WRITE_CALENDAR);
这意味着你需要检查你的应用程序是否有使用危险权限的权限。
activitycompat.requestPermission()则用于请求用户允许我们使用危险权限。
下面是代码片段:
//checking if you have the permission
if (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) {
   // you don't have permission so here you will request the user for permission

  if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
        Manifest.permission.READ_CONTACTS)) {

        // Show an explanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.

    } else {

        // No explanation needed, we can request the permission.

        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.READ_CONTACTS},
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);

        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }

}

我希望这能在一定程度上澄清你的概念。


谢谢回答...我明白了这个概念,但在你上面的那一行中,“这意味着你要检查你的应用程序是否有权限使用危险权限。” 我是在检查应用程序运行的Android版本吗? - user7064261
@SaathwikNalige,checkSelfPermission() 只能告诉你你的应用程序是否被授予所请求的权限,而没有进行任何 Android 版本检查。 - Harsh Sharma
@HarshSharma 谢谢你的回答...首先,我要检查我的应用程序是否有读取联系人的权限,然后再向用户请求相同的权限,对吗? - user7064261

1

ContextCompat.checkSelfPermission(Context context, String permission)

此方法用于检查您的应用程序是否具有所请求的权限。

ActivityCompat.requestPermissions(Activity activity, String[] permissions,int reqCode)

此方法用于请求权限。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接