ACCESS_FINE_LOCATION permissions

前端 未结 1 1554
悲哀的现实
悲哀的现实 2020-12-22 08:18

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

My program w

相关标签:
1条回答
  • 2020-12-22 09:13

    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.

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

    Something like this:

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

    The verification steps are roughly something like the following:

    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.

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

    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.

    0 讨论(0)
提交回复
热议问题