3

I used ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION); to check my app has location permission or not.This method returns PackageManager.PERMISSION_GRANTED which is correct answer.Now I go to my app's settings and disable location permision.Now also the above method returns PackageManager.PERMISSION_GRANTED. But it should PackageManager.PERMISSION_DENIED.Can anyone give me some idea in this matter?If I can use another method what is it?

Homen
  • 1,222
  • 1
  • 12
  • 16

2 Answers2

3

To request permission

if (checkSelfPermission(Manifest.permission.CAMERA) 
        == PackageManager.PERMISSION_DENIED) {
  requestPermissions(new String[]{Manifest.permission.CAMERA}, YOUR_REQUEST_CODE);
}

and to check if user has accepted your request override the following code

@Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
    switch (requestCode) {
        case YOUR_REQUEST_CODE: {
            if (checkSelfPermission(Manifest.permission.CAMERA) == 
PackageManager.PERMISSION_GRANTED){
                // permission was granted
            } else {
                // permission denied
                //Disable the functionality 
                //that depends on this permission.
            }
            return;
        }
    }
}

Ref :http://sourabhkarkal.com/blogspot/requesting-permissions-in-android-m/

Auto-Droid ツ
  • 1,583
  • 1
  • 17
  • 31
  • 1
    I am also using this code.But after disable permission from app settings it return PackageManager.PERMISSION_GRANTED.After disable it should be PackageManager.PERMISSION_DENIED.Is not it? – Homen Jun 28 '16 at 07:01
  • On your activity OnResume method do checkSelfPermission() and tell me the result – Auto-Droid ツ Jun 28 '16 at 07:03
1

As per your comments, the target sdk is 22. So the method checkSelfPermission always gives you permission as Granted. If you want to implement Runtime permission, you have to change it to 23.

If you want detail answer, please refer SO question. Read all comments.

Community
  • 1
  • 1
Mani
  • 17,549
  • 13
  • 79
  • 100