Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Security Exception only on Android 6

java.lang.SecurityException: Client must have ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to perform any location operations.

The app works fine for Android version 5(Lollipop) but gets a Security Exception on the Android version 6.

like image 442
user1382802 Avatar asked Oct 11 '15 09:10

user1382802


2 Answers

On your activity:

private static final int REQUEST_FINE_LOCATION=0
...
loadPermissions(Manifest.permission.ACCESS_FINE_LOCATION,REQUEST_FINE_LOCATION);

Implementation:

private void loadPermissions(String perm,int requestCode) {
    if (ContextCompat.checkSelfPermission(this, perm) != PackageManager.PERMISSION_GRANTED) {
        if (!ActivityCompat.shouldShowRequestPermissionRationale(this, perm)) {
            ActivityCompat.requestPermissions(this, new String[]{perm},requestCode);
        }
    }
}

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

    }

}
like image 125
Hpsaturn Avatar answered Oct 30 '22 06:10

Hpsaturn


This is because these permissions are dangerous permission as per new permission model of android 6.0.

dangerous permissions need to be asked for at run time and should be checked every time before usage.

you will have to change your implementation to adapt new permission model.

like image 34
Rahul Tiwari Avatar answered Oct 30 '22 06:10

Rahul Tiwari