1

I'm using the Google location API to show the users location on the map.

When the app loads for the first time, rationale dialog is displayed explaining why user needs to enable access to location. Then, runtime permission dialog is shown, and users clicks “Allow” to enable location access. The Map then loads fine without any crashes.

However, when the app resumes (after going into background), no rationale dialog appears since user has already granted location access. In this scenario, only the runtime permission dialog appears. Now if the user clicks “Deny”, the app crashes. This code works fine in Android versions below M (Lollipop, Jellybean, KitKat etc.)

Is there a way to handle the runtime exception at this stage?

The error is :

java.lang.SecurityException: Client must have ACCESS_FINE_LOCATION permission to request PRIORITY_HIGH_ACCURACY locations.

I'm using the standard Sample MyLocation demo app without any 3rd party library:

GoogleMaps MapDemo on Github

I've also tried using the PermissionsDispatcher Library, but the same errors persist.

PermissionDispatcher Android-Google-Maps-Demo

private void enableMyLocation() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {
        // Permission to access the location is missing.
        PermissionUtils.requestPermission(this, LOCATION_PERMISSION_REQUEST_CODE,
                Manifest.permission.ACCESS_FINE_LOCATION, true);
    } else if (mMap != null) {
        // Access to the location has been granted to the app.
        mMap.setMyLocationEnabled(true);
    }
}

Any help is highly appreciated.

Akshata
  • 955
  • 1
  • 11
  • 20
  • Post the code where you request permissions please – Juan Cruz Soler Jun 27 '16 at 19:01
  • Implementation of accepting/denying the permission doesn't means that your code will run fine. It only depends on what code you wrote in those cases. Please post the onCreate() and request permissions callback methods? – Shadab Ansari Jun 27 '16 at 19:10
  • I am using the exact example given in the [link](https://github.com/googlemaps/android-samples/blob/master/ApiDemos/app/src/main/java/com/example/mapdemo/MyLocationDemoActivity.java) – Akshata Jun 27 '16 at 19:18
  • The if statement returns false when the activity resumes and the runtime permission dialog is shown on the line mMap.setMyLocationEnabled(true); line. If I choose "Deny" on the permissions dialog, the app crashes.The callback method is not called at this point. – Akshata Jun 27 '16 at 19:27

2 Answers2

0

Make sure about that you have add right permission in your Manifest files about what permission you are asking and needed exactly

And may be problem with your code. Try this library: RuntimePermission Library To ask permission manually.

Uttam Panchasara
  • 5,420
  • 4
  • 24
  • 41
0

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. This approach streamlines the app install process, since the user does not need to grant permissions when they install or update the app. It also gives the user more control over the app's functionality.

How to check if you have the permission:

// Assume thisActivity is the current activity
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.ACCESS_FINE_LOCATION);
How to request the permissions you need :

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

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

// MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}

How to access the result of the request :

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {

// permission was granted, yay! Do the task you need to do.

} else {

// permission denied, boo! Disable the functionality that depends on this permission.
}
return;
}
}
}

Here's a related SO ticket: Google Maps API Android v2 - "ACCESS_FINE_LOCATION" permission required with my-location layer enabled

Community
  • 1
  • 1
Android Enthusiast
  • 4,635
  • 2
  • 13
  • 29