-1

on Android, I check if the location is enabled with

LocationManager.isProviderEnabled(GPS_PROVIDER) || LocationManager.isProviderEnabled(NETWORK_PROVIDER)

This work fine, however on marshmallow (and upper) when a user go in the app settings and deny only for my app the permissions to use the location (just for my app, like mashmallow now permit to do) then the previous request still return true

I try also :

MyActivity.checkSelfPermission('android.permission.ACCESS_FINE_LOCATION') == PERMISSION_GRANTED or MyActivity.checkSelfPermission('android.permission.ACCESS_COARSE_LOCATION') == PERMISSION_GRANTED

But it's always return true even when the user deny the permission to my app

Shabbir Dhangot
  • 8,954
  • 10
  • 58
  • 80
zeus
  • 12,173
  • 9
  • 63
  • 184
  • What is your `targetSdkVersion` set to? – Michael Sep 29 '16 at 08:53
  • to 14 (and i can't increase it right now) but normally it's must not matter no ? because checkSelfPermission was only introduced on api 23 so their is no raison to make it behave differently if targetSdkVersion < 23 ... i think ! – zeus Sep 29 '16 at 10:15

4 Answers4

1

Make sure you are have compileSdkVersion and targetSdkVersion set to 23 in your build.gradle. If it is below 23, the app will use the old permission approach and the methods you mentioned will not work.

Kelevandos
  • 7,024
  • 2
  • 29
  • 46
  • thanks kelevandos, but why this can't work with targetSdkVersion < 23 ? increasing targetSdkVersion to 23 gave me some other problem i don't want to face right now :( – zeus Sep 29 '16 at 10:21
  • Backward compatibility, they did not want to break the old permission handling. They still kind of did, but well xD – Kelevandos Sep 29 '16 at 10:22
  • why Backward compatibility ? because checkSelfPermission was introduced on api 23 so their is no need of Backward compatibility ? – zeus Sep 29 '16 at 11:29
  • But you still want apps that were developed before 23 to work when installed on 23+ :-) So the new approach kind of "wraps" the old, assuming that the permissions are always given :-) – Kelevandos Sep 29 '16 at 12:51
1

Try with following code:

ContextCompat.checkSelfPermission(thisActivity,   Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED
Arjun
  • 736
  • 1
  • 8
  • 24
Nguyễn Hoàng
  • 179
  • 1
  • 11
1

Please refer this link. Make sure you have compileSdkVersion should be 23 and above.

Here i have created the code with checking multiple runtime permission with should show rational.

Arpit Patel
  • 1,561
  • 13
  • 23
  • yes compileSdkVersion is 23 (else i will not be able to access the checkSelfPermission i think) – zeus Sep 29 '16 at 10:16
0

checkSelfPermission doesn't return a boolean. It returns an integer.

This is the correct way to check permissions:

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

    // permission is not granted, request it
    ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);

} else {    
    //permision granted
}

And this is the way for handle them

@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
                // 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
    }
}

From documentation

https://developer.android.com/training/permissions/requesting.html

adalpari
  • 3,052
  • 20
  • 39
  • yes it's like this i do : MyActivity.checkSelfPermission('android.permission.ACCESS_FINE_LOCATION') == PERMISSION_GRANTED but it's return true even when user deny the permissions :( – zeus Sep 29 '16 at 10:18
  • Have you tryed call it from *ContextCompat* instead of *MyActivity*. Can you show us your code? – adalpari Sep 29 '16 at 10:41