对 android 运行时权限感到困惑

confused about android runtime permissions

所以这个全新的 android 运行时权限让我感到困惑。我的应用目前正在编译和定位版本 23,这意味着我必须使用运行时权限。我的应用程序主要使用需要相机权限的相机 api 所以我在打开相机之前添加了运行时权限:

        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED)
        {//ask permissions for camera
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.CAMERA},
                    CameraPermissions);
        }
        else
        {//permissions attained now you can open the camera
            camera=Camera.open(getCid());
            camera.setPreviewCallback(this);
            initPreview(width, height);
            startPreview();
            startTimer();
        }

我也检查何时停止相机:

       if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.CAMERA)
                == PackageManager.PERMISSION_GRANTED) {
            camera.setPreviewCallback(null);
            camera.release();
            faceProc.release();
            faceProc = null;
            camera = null;
            inPreview = false;
            cameraConfigured = false;
        }

权限请求是这样处理的:

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

                StartUpCam();
            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("How is this app going to work if you rejected the camera permission.... DUHHHH!!")
                        .setTitle("Rejected");
                builder.setPositiveButton("Exit App", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        //close application
                        closeApp();
                    }
                });
                AlertDialog dialog = builder.create();
                dialog.show();
            }
            return;
        }
    }
}

因此,当发出请求时,它会调用 StartUpCam,然后在获得权限后尝试打开相机。所以我的问题来了,如果我添加这个运行时权限检查这会如何影响 android 低于 6.0 的设备?所以5.0.1版本的phone也会提示给相机权限?如果我使用运行时权限,是否必须在清单文件中删除相机权限?目前,我将相机权限与运行时权限一起保留在清单中,我不知道这是否正确。如果我降低目标并将 sdk 编译为 22 而不是 23,那么 android 6.0 以上的设备将无法下载我的应用程序吗???如果我将它降低到版本 22,那么我就避免了所有这些令人头疼的事情......

I also check when I stop the camera

假设您没有尝试停止从未打开过的相机,则不需要这样做。如果用户在您的应用 运行 时撤销权限,您的进程将立即终止。因此,您永远不会失去 运行 应用程序的权限。由于您检查并获得了打开相机的权限,因此您已经拥有关闭它的权限。

if I add this runtime permission checks how does this affect android devices lower than 6.0?

ContextCompat.checkSelfPermission() 将在旧设备上 return PackageManager.PERMISSION_GRANTED,前提是您拥有清单中列出的权限。

So a phone with version 5.0.1 will also get a prompt to give camera permissions?

没有

If I use runtime permissions, do I have to remove the camera permissions in the manifest file?

没有。所有 Android 版本都需要这些元素。

What if I lower the target and compiling sdk to 22 instead of 23, will android devices above 6.0 won't be able to download my app?

您的 compileSdkVersion 对您支持的 Android 版本没有影响。 Android6.0 用户仍然可以下载您的应用程序。

如果您将 targetSdkVersion 降低到 22 或更低,这对您支持的 Android 版本也没有影响。 Android 6.0 用户仍然可以下载您的应用。这样做意味着您可以跳过运行时权限代码。但是,请记住,您仍然可能没有权限。 Android 6.0 设备的用户,运行 您的 targetSdkVersion 22 应用程序,将默认授予 CAMERA 权限。但是,这些用户仍然可以进入“设置”>“应用”,找到您的应用,然后撤销许可。有了摄像头API,基本开不了摄像头

战术上,targetSdkVersion 22 或更低当然是可能的。不过,最终,某些事情会 "force your hand" 并要求您移动到 ​​targetSdkVersion 23 或更高。因此,总有一天,您将需要处理运行时权限。是今天还是未来某天,由您决定。

否,如果权限在清单文件中。 Android 小于23的sdk不会提示权限,和之前一样。