检查 android 台设备中应用程序的运行时权限

Check the runtime permission of app in android device

我正在使用以下代码片段来了解用户设置的权限,因为在较新的 android 设备权限可以针对特定应用程序从设置中进行调整。

我想提醒用户授予权限以避免应用程序崩溃但是以下代码段对我来说总是返回 true。我做错了什么?

//If authorisation not granted for camera
boolean permission = (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)==PackageManager.PERMISSION_GRANTED);


if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
    //ask for authorisation
    //Manifest.permission.CAMERA
    if (ActivityCompat.shouldShowRequestPermissionRationale(this,
            Manifest.permission.CAMERA)) {
        showExplanation("Permission Needed", "Rationale", Manifest.permission.CAMERA, REQUEST_PERMISSION_CAMERA);
    }
    else
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, REQUEST_PERMISSION_CAMERA);
}
else
    try {
        //releasing camera if it's already in use
        releaseCamera();
        camera = Camera.open(camId);
}catch (Exception e)
{
    e.printStackTrace();
}
boolean permission = (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)==PackageManager.PERMISSION_GRANTED);

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
    //ask for authorisation
    //Manifest.permission.CAMERA
    if (ActivityCompat.shouldShowRequestPermissionRationale(this,
            Manifest.permission.READ_PHONE_STATE)) {
        showExplanation("Permission Needed", "Rationale", Manifest.permission.READ_PHONE_STATE, REQUEST_PERMISSION_CAMERA);
    }
    else
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, REQUEST_PERMISSION_CAMERA);
}
else{
    try {
        //releasing camera if it's already in use
        releaseCamera();
        camera = Camera.open(camId);
}catch (Exception e)
{
    e.printStackTrace();
}}   /////// put your else condition in braces

我遇到了这个问题,因为此功能仅在 api 23 级及以上可用。我在 22 日编译。