对Android运行时权限感到困惑 [英] confused about android runtime permissions

查看:85
本文介绍了对Android运行时权限感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,全新的android运行时权限使我感到困惑.我的应用当前正在编译和定位版本23,这意味着我必须使用运行时权限.我的应用主要使用需要相机权限的相机API,因此我在打开相机之前先添加了运行时权限:

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

因此,在发出请求时,它会调用StartUpCam,如果授予了许可,它会尝试打开摄像头.所以这是我的问题,如果我添加此运行时权限,将检查这如何影响低于6.0的android设备?因此,版本5.0.1的手机还会提示您授予摄像头权限吗?如果我使用运行时权限,是否必须在清单文件中删除摄像头权限?当前,我将相机权限和清单中的运行时权限一起保存在清单中,我不知道这是否正确.如果我将目标降低并将sdk编译为22(而不是23),该怎么办?6.0以上的android设备将无法下载我的应用程序???如果我将其降低到版本22,那么可以避免所有这些头痛的事情...

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...

推荐答案

我还会检查何时停止相机

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.

如果我添加此运行时权限,则会检查这如何影响低于6.0的android设备?

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

ContextCompat.checkSelfPermission()在较旧的设备上将返回PackageManager.PERMISSION_GRANTED,前提是您具有清单中列出的权限.

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

那么5.0.1版的手机还会提示您授予摄像头权限吗?

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版本上都是必需的.

No. Those elements are necessary on all Android versions.

如果我将目标降低并将sdk编译为22(而不是23)怎么办?6.0版以上的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版本没有影响. Android 6.0用户仍然可以下载您的应用.

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

如果将targetSdkVersion降低到22或更低,这也不会影响您支持的Android版本. Android 6.0用户仍然可以下载您的应用.这样做将意味着您可以跳过运行时权限代码.但是,请记住,您可能仍未获得许可.默认情况下,运行您的targetSdkVersion 22应用程序的Android 6.0设备的用户将授予CAMERA权限.但是,这些用户仍然可以进入设置">应用",找到您的应用,然后撤消该权限.使用相机API,您基本上无法打开相机.

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.

从策略上讲,targetSdkVersion为22或更低肯定是可能的.但是,最终,有些事情会迫使您动手",并要求您将targetSdkVersion移至23或更高.因此,有一天,您将需要处理运行时权限.无论是今天还是将来的一天,都由您决定.

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.

这篇关于对Android运行时权限感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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