3

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);
Darian B
  • 317
  • 2
  • 3
  • 15
  • Please post your code also – Sahil Garg Jan 01 '16 at 07:06
  • @SahilGarg is this good? – Darian B Jan 01 '16 at 07:10
  • try to add one more permission that is ACCESS_COARSE_LOCATION – Sahil Garg Jan 01 '16 at 07:24
  • What do you mean by, "doesn't enable location like it should."? What are you expecting and what is actually happening? – Larry Schiefer Jan 02 '16 at 06:54
  • @LarrySchiefer i want to do mMap.setMyLocationEnabled(true) on the first time the user launches my app. So what is currently happening is when i clear all data on the app and launch it, it opens to a map view and should show a blue dot for my location. It opens and requests permission, I press allow. Then it doesn't enable my location, but it does if i kill the app and open it again. – Darian B Jan 02 '16 at 07:27
  • You shouldn't have to check for the permission again in your `onRequestPermissionResult()`. If that callback is receiving `GRANTED` then you have it. I suspect the call to `checkSelfPermission()` bring done in that handler is throwing things of with the logic that is there. – Larry Schiefer Jan 02 '16 at 14:43

3 Answers3

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.

Community
  • 1
  • 1
0

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.

0

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.

IRPdevelop
  • 281
  • 2
  • 13