如何在运行时检查授予权限? [英] How to check Grants Permissions at Run-Time?

查看:104
本文介绍了如何在运行时检查授予权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android M(预览版)中,用户可以选择特定的应用并获取特定的权限.

In Android M (Preview) the user can choose a specific app and retreive specific permission.

所以我要问如何在运行时检查Grants权限?

推荐答案

不错!

我刚刚发现了我的需要,我们可以检查该权限是否由授予:

I just found my need we can check if the permission is granted by :

checkSelfPermission(Manifest.permission.READ_CONTACTS)

如有必要,请求权限

if (checkSelfPermission(Manifest.permission.READ_CONTACTS)
            != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(new String[]{Manifest.permission.READ_CONTACTS},
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);

        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant

        return;
    }

处理权限请求响应

@Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! do the
                // calendar task you need to do.

            } else {

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

        // other 'switch' lines to check for other
        // permissions this app might request
    }
}

这篇关于如何在运行时检查授予权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆