ACCESS_FINE_LOCATION权限 [英] ACCESS_FINE_LOCATION permissions

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

问题描述

我一直在尝试找出如何在API 23+上正确设置CAMERAACCESS_FINE_LOCATION的android权限.

I have been trying to figure out how to properly setup the android permissions for CAMERA and ACCESS_FINE_LOCATION on API 23+.

我的程序将请求CAMERA,但在运行时将不请求ACCESS_FINE_LOCATION权限.

My program will request the CAMERA, but it will not request the ACCESS_FINE_LOCATION permission during runtime.

我已阅读许可

I have read permissions and location developers page, but I cannot seem to grasp how to request the permissions properly. I have also looked through stackoverflow for answers, but still do not understand.

有人可以在我的代码中解释我做错了什么,以及如何解决它吗?

Would someone be able to explain what I am doing wrong in my code, and how to fix it?

我的代码:

    private void connectCamera() {
    CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) ==
                    PackageManager.PERMISSION_GRANTED) {
                cameraManager.openCamera(mCameraId, mCameraDeviceStateCallback, mBackgroundHandler);
            } else {
                if (shouldShowRequestPermissionRationale(android.Manifest.permission.CAMERA))  {
                    Toast.makeText(this,
                            "Video app required access to camera and location", Toast.LENGTH_SHORT).show();
                }
                requestPermissions(new String[]{android.Manifest.permission.CAMERA,
                        android.Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.ACCESS_COARSE_LOCATION
                }, REQUEST_CAMERA_PERMISSION_RESULT);
            }

        } else {
            cameraManager.openCamera(mCameraId, mCameraDeviceStateCallback, mBackgroundHandler);
        }
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}


    @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == REQUEST_CAMERA_PERMISSION_RESULT) {
        if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(getApplicationContext(),
                    "Application will not run without camera and location services!", Toast.LENGTH_SHORT).show();
        }
    }
}

推荐答案

您不会在所提供的代码中的任何地方询问权限.要请求位置功能,您可以尝试以下示例:

you're not asking for the permissions anywhere in the code you provided. To ask for the location feature, you can try the example below:

ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                REQUEST_CODE_FINE_GPS);

此外,一旦用户授予使用此功能的权限,您就不会使应用打开相机.

Also, you're not making your app open the camera once the user gives the permission to use this feature.

在您的onRequestPermissionsResult()方法内,您应该添加一个else调用openCamera.

Inside your onRequestPermissionsResult() method, you should add an else calling openCamera.

类似这样的东西:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == REQUEST_CAMERA_PERMISSION_RESULT) {
        if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(getApplicationContext(),
                    "Application will not run without camera and location services!", Toast.LENGTH_SHORT).show();
        }
        else {
            cameraManager.openCamera(mCameraId, mCameraDeviceStateCallback, mBackgroundHandler);
        }
    }
}

验证步骤大致如下:

  1. 打开应用后,检查API> = 23
  2. 如果它的API> = 23,请检查是否已授予该权限.
  3. 如果不是,请寻求许可.
  4. 请求的结果将在onRequestPermissionsResult()方法上.您必须在那里处理用户输入.如果他没有得到许可,并且此功能对于您的应用程序运行,显示对话框或其他解释他为什么要授予您许可的原因是必不可少的,然后再次询问.
  5. 如果获得许可,则可以获利.在您的应用上调用将使用该功能的方法.
  1. When the app opens, check if API >= 23
  2. If it's API >= 23, check if the permission is already granted.
  3. If it isn't, ask for the permission.
  4. The result from the request will be on the onRequestPermissionsResult() method. You have to handle the user input there. If he didn't gave permission, and this feature is essential for your app to run, show a dialog, or something else explaining why he should give you the permission, and ask again.
  5. If the permission was given, profit. Call the method on your app that will use that feature.

看看此答案.那家伙创建了一个很好的帮助程序方法来检查并询问您可能需要的任何数量的权限.然后,您只需要在onRequestPermissionsResult()上处理它们即可.

Take a look at this answer. That guy created a nice helper method to check and ask for any number of permissions you might need. Then, you'll just have to handle them on the onRequestPermissionsResult().

PS:强烈建议您重新阅读有关如何在API 23+上请求权限的文档,您的问题表明您不了解权限请求的基本概念.另外,请查看Android培训页面的此链接,并

P.S.: I strongly recommend that you re-read the docs on how to request permissions on API 23+, your question shows that you didn't understand the basic concepts of permission requesting. Also, check this link of the Android training page, and this related question.

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

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