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
  • 1,005
  • 2
  • 12
  • 22
  • 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

1 Answers1

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,735
  • 5
  • 25
  • 41