confused about android runtime permissions

2019-02-26 11:17发布

问题:

So this whole new android runtime permissions has gotten me confused. My app is currently compiling and targetting version 23 which means I have to use runtime permissions. My app primarily uses the camera api which needs the camera permission so I added the runtime permissions before opening the camera as such:

        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();
        }

I also check when I stop the camera:

       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;
        }

The permission request is handled as such:

@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;
        }
    }
}

So when the request is given it calls the StartUpCam which then tries to open the camera if the permissions is given. So here comes my questions, if I add this runtime permission checks how does this affect android devices lower than 6.0?? 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? Currently, I keep the camera permissions in the manifest along with the runtime permissions I don't know if that is correct or not. 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??? If I lower it to version 22 then I avoid all this headache...

回答1:

I also check when I stop the camera

That is not needed, assuming that you do not try to stop a camera that you never opened. If the user revokes the permission while your app is running, your process is immediately terminated. As a result, you can never lose permissions in a running app. Since you checked for and had permission to open the camera, you already have permission to close it.

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

ContextCompat.checkSelfPermission() will return PackageManager.PERMISSION_GRANTED on older devices, assuming that you have the permission listed in the manifest.

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

No.

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

No. Those elements are necessary on all Android versions.

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?

Your compileSdkVersion has no impact on what versions of Android you support. Android 6.0 users will still be able to download your app.

If you lower your targetSdkVersion to 22 or lower, that too has no impact on what versions of Android that you support. Android 6.0 users will still be able to download your app. Doing this would mean that you could skip the runtime permission code. However, bear in mind that you still may not have permission. Users of Android 6.0 devices, running your targetSdkVersion 22 app, will grant the CAMERA permission by default. But, those users can still go into Settings > Apps, find your app, and revoke the permission. With the camera API, you basically cannot open the camera.

Tactically, going with targetSdkVersion of 22 or lower is certainly possible. Eventually, though, something is going to "force your hand" and require you to move to a targetSdkVersion of 23 or higher. So, someday, you will need to deal with the runtime permissions. Whether that is today or some day in the future is up to you do decide.



回答2:

No if the Permission is in Menifest file. Android having less than 23 sdk won't receive a prompt about the permission, It will be the same like before.