首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android检查权限

问Android检查权限
EN

Stack Overflow用户
提问于 2015-11-05 09:25:10
回答 3查看 19.2K关注 0票数 2

我正在permissions 23中开发我的项目,其中新引入了应用程序权限。在一些指南中,他们使用下面的代码读取电话状态,是否授予了权限。

代码语言:javascript
复制
if (ContextCompat.checkSelfPermission(serviceContext, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
    //Read Phone state
   }else{
}

但我正在直接访问checkSelfPermission,如下所示

代码语言:javascript
复制
if(serviceContext.checkSelfPermission(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
      //Read Phone state
   }else{
}

它很好用。我的问题是,上面这些codes?.which之间有什么区别,是检查许可是否被授予的正确方式吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-11-05 09:46:06

我的问题是,以上这些代码之间有什么区别?

没有,在API 23(+)设备上。

然而,运行较旧版本安卓的设备在尝试直接调用context.checkSelfPermission()时会产生错误。此方法直到API 23才可用。

ContextCompat还提供了一种向后兼容的方式来在旧API上运行checkSelfPermission()。如果您查看实现,您将看到它通过使用应用程序自己的流程参数将调用委托给checkPermission()来实现这一点。checkPermission()自第一个API发布以来就已经可用,因此它将全面运行。

代码语言:javascript
复制
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());
}

哪一种检查批准许可的方法是正确的?

因此,要回答这个问题:如果您只支持运行Android 'Marshmallow‘6.0及更新版本的设备,那么您可以使用任何一种方法。但是,由于更有可能您也希望支持一些旧版本的Android,所以使用

票数 8
EN

Stack Overflow用户

发布于 2015-11-05 09:31:30

在官方和近期方式上编写代码,以便在下面使用的所有设备中支持

请求需要的权限

代码语言:javascript
复制
 // Here, thisActivity is the current activity
    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 expanation 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.
        }
    }

处理权限请求响应

代码语言:javascript
复制
@Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}
票数 4
EN

Stack Overflow用户

发布于 2017-03-10 09:09:23

另一种解决办法是:

代码语言:javascript
复制
//Requesting permission
private void requestStoragePermission(){

    if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.READ_EXTERNAL_STORAGE)){
        //If the user has denied the permission previously your code will come to this block
        //Here you can explain why you need this permission
        //Explain here why you need this permission
    }

    //And finally ask for the permission
    ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},STORAGE_PERMISSION_CODE);
}

//This method will be called when the user will tap on allow or deny
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

    //Checking the request code of our request
    if(requestCode == STORAGE_PERMISSION_CODE){

        //If permission is granted
        if(grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){

            //Displaying a toast
            Toast.makeText(this,"Permission granted now you can read the storage",Toast.LENGTH_LONG).show();
        }else{
            //Displaying another toast if permission is not granted
            Toast.makeText(this,"Oops you just denied the permission",Toast.LENGTH_LONG).show();
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33540755

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档