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?
Asked
Active
Viewed 2,509 times
3

Homen
- 1,222
- 1
- 12
- 16
-
are you sure you are using marshmallow device – JAAD Jun 28 '16 at 06:49
-
you are checking this status in onCreate or onResume – Vishal Thareja Jun 28 '16 at 06:49
-
Could you post your code? I mean, the code of `onRequestPermissionsResult`.. – Mani Jun 28 '16 at 06:50
-
@ankit Yes I am using marshmalow device – Homen Jun 28 '16 at 06:51
-
@vishal I checked this status in onCreate .Is anything wrong? – Homen Jun 28 '16 at 06:52
-
@Homen This happening might be you havn't kill you app, and open setting then disable location permission, again open activity, but in onresume you havn't check as oncreate is not called so its returning true – Vishal Thareja Jun 28 '16 at 06:55
-
@mani I am not imeplemented onRequestPermissionsResult yet.It is only Handle the permissions request response.Is not it? – Homen Jun 28 '16 at 06:57
-
@Homen What is your target SDK? 23 or below 23? – Mani Jun 28 '16 at 07:01
-
@Mani it is 22.Should I change it to 23 – Homen Jun 28 '16 at 07:07
-
@Homen Please see my answer. – Mani Jun 28 '16 at 07:13
2 Answers
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
-
1I 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.