When asking for runtime permission for location

雨燕双飞 提交于 2019-12-12 14:23:32

问题


Currently when asking for run time permission when launching my app for the very first time it prompts the user to use their location. And if you click yes it doesn't enable location like it should.

But if I relaunch the app it enables the location. Any suggestions as to where I can get it to have location enabled on first launch ? The first part of the code is called in OnCreate.

    if (ContextCompat.checkSelfPermission(MapsActivity.this,
            Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(MapsActivity.this,
                Manifest.permission.ACCESS_FINE_LOCATION)) {

            // Show an expanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.

        } else {

            // No explanation needed, we can request the permission.

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

        }
    }

  @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == REQUEST_CODE_ASK_PERMISSIONS) {
        if (permissions.length == 1 &&
                permissions[0] == Manifest.permission.ACCESS_FINE_LOCATION &&
                grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                    == PackageManager.PERMISSION_GRANTED) {
                mMap.setMyLocationEnabled(true);
            } else {
                // Show rationale and request permission.
            }
            mMap.setMyLocationEnabled(true);
        } else {
            //permission denied
        }
    }
}

  public void onMapReady(GoogleMap googleMap) {

    int permissionCheck = ContextCompat.checkSelfPermission(MapsActivity.this,
            Manifest.permission.ACCESS_FINE_LOCATION);

回答1:


If you are testing on an emulator the permission will only be asked on the first run. How to debug Android 6.0 permissions?

Also if you are not completely uninstalling the app and it is being upgraded from a lower SDK or permissions have been set by the user at runtime and made default, then the runtime permissions will not be asked again.

Try completely uninstalling the app, or checking the default settings and clearing them.




回答2:


Take a closer look at this line

permissions[0] == Manifest.permission.ACCESS_FINE_LOCATION

permissions is a String array meaning that you should compare using equals() or compareTo() method.




回答3:


If this code is in a Fragment, OnRequestPermissionResult() won't be called. So setMyLocationEnabled won't be called until you re open the app.

To fix this change:

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

To:

requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                REQUEST_CODE_ASK_PERMISSIONS);

ActivityCompat.requestPermissions is for Activities. When you use a Fragment you should call requestPermissions directly.



来源:https://stackoverflow.com/questions/34554263/when-asking-for-runtime-permission-for-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!