如何使用ContextCompat.checkSelfPermission()检查运行时的android应用程序权限?

时间:2017-07-03 07:24:44

标签: android permissions

需要根据android 6及以上标准检查android应用程序用户是否授予所需权限(特别是读取用户设备短信)。我特意在寻找android开发者网站上描述的ContextCompat.checkSelfPermission()方法。 看这里: link

3 个答案:

答案 0 :(得分:1)

请关注this tutorials这是完整的源代码

static final int PERMISSION_ALL = 1;
    String[] PERMISSIONS =
            {Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION};

     protected void runtimePermission() {
        if (!hasPermission(ContactUS.this, PERMISSIONS)) {
            ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
        }
    }

    public static boolean hasPermission(Context context, String... permissions) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
            for (String permission : permissions) {
                if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                    return false;
                }

            }
        }
        return true;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case PERMISSION_ALL:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    //permission granted
                    //your logic here
                } else {
                    //permission denied
                }
                break;

        }
    }

答案 1 :(得分:1)

您可以像这样添加运行时权限 按照以下步骤

第1步: - 首先在最明显的文件中添加此权限

android.Manifest.permission.ACCESS_FINE_LOCATION,
android.Manifest.permission.ACCESS_COARSE_LOCATION,

现在,而不是

第2步:询问运行时权限

String permission = android.Manifest.permission.ACCESS_FINE_LOCATION;
        if (ActivityCompat.checkSelfPermission(SearchCityClass.this, permission)
                != PackageManager.PERMISSION_GRANTED && ActivityCompat.
                checkSelfPermission(SearchCityClass.this, android.Manifest.permission.ACCESS_COARSE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(SearchCityClass.this, new String[]
                    {permission}, PERMISSION_GPS_CODE);

        }

第3步:在onRequestPermissionsResult中正确处理permsiion结果

 @Override
 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                   @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSION_GPS_CODE) {
    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {


        Toast.makeText(this, location_permission_granted_msg, Toast.LENGTH_SHORT).show();

    } else {

        Toast.makeText(this, location_permission_not_granted_msg, Toast.LENGTH_SHORT).show();
    }
}

}

答案 2 :(得分:0)

如果通过以下方式授予许可:

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 granted */

            } else {

                /* permission denied */
            }
            return;
        }

        /* check for other permissions */
    }
}

你也可以使用像this这样的库,无论如何祝你好运!