Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Client must have ACCESS_FINE_LOCATION permission to request PRIORITY_HIGH_ACCURACY locations in android 6

I have a problem that happen recently on devices with android version 6.0

I am getting the following crash

java.lang.SecurityException: Client must have ACCESS_FINE_LOCATION permission to request PRIORITY_HIGH_ACCURACY locations.
at android.os.Parcel.readException(Parcel.java:1627)
at android.os.Parcel.readException(Parcel.java:1579)
at ub.a(:com.google.android.gms.DynamiteModulesB:1191)
at to.a(:com.google.android.gms.DynamiteModulesB:2110)
at lw.b(:com.google.android.gms.DynamiteModulesB:148)
at ml.b(:com.google.android.gms.DynamiteModulesB:1164)
at nf.b(:com.google.android.gms.DynamiteModulesB:260)
at na.b(:com.google.android.gms.DynamiteModulesB:324)
at tn.a(:com.google.android.gms.DynamiteModulesB:78)
at maps.af.L.a(Unknown Source)
at na.a(:com.google.android.gms.DynamiteModulesB:13201)
at mo.g(:com.google.android.gms.DynamiteModulesB:499)
at mo.a(:com.google.android.gms.DynamiteModulesB:435)
at mh.a(:com.google.android.gms.DynamiteModulesB:1437)
at pj.a(:com.google.android.gms.DynamiteModulesB:227)
at oz.a(:com.google.android.gms.DynamiteModulesB:780)
at oq.a(:com.google.android.gms.DynamiteModulesB:1829)
at ot.handleMessage(:com.google.android.gms.DynamiteModulesB:5339)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:234)
at android.app.ActivityThread.main(ActivityThread.java:5526)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

can anyone help please ?

like image 616
Amira Elsayed Ismail Avatar asked Dec 03 '22 14:12

Amira Elsayed Ismail


2 Answers

Here is a sample code that you can use to request for Runtime permissions:

private static final int MY_PERMISSIONS_REQUEST_READ_FINE_LOCATION = 100;

and

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
            Manifest.permission.ACCESS_FINE_LOCATION)
    != PackageManager.PERMISSION_GRANTED &&
                    ContextCompat.checkSelfPermission( getApplicationContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {

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

      // Show an explanation 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(thisActivity,
            new String[]{Manifest.permission.READ_CONTACTS},
            MY_PERMISSION_REQUEST_READ_FINE_LOCATION);

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

Then you can receive the request here:

@Override
public void onRequestPermissionsResult(int requestCode,
    String permissions[], int[] grantResults) {
    switch (requestCode) {
    case MY_PERMISSION_REQUEST_READ_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 contacts-related task you need to do.

        } else {

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

    // other 'case' lines to check for other
    // permissions this app might request
  }
 }

You can easily call this in your activity but I would highly recommend you read the documentation here to get a better idea.

I hope this helps!

like image 110
Eenvincible Avatar answered Dec 06 '22 10:12

Eenvincible


Your Android 6 users can revoke the permissions of your application selectively, so you must ask for permissions at runtime.

An example from the documentation:

private static final int MY_PERMISSIONS_REQUEST_FINE_LOCATION = 111;

// ...

// Check for permission
if (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.ACCESS_FINE_LOCATION)
        != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(
            this, // Activity
            new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                MY_PERMISSIONS_REQUEST_FINE_LOCATION);
    }
}

// Get permission result
@Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_FINE_LOCATION: {
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // permission was granted

            } else {
                // permission was denied
            }
            return;
        }
    }
}
like image 35
antonio Avatar answered Dec 06 '22 10:12

antonio