-1

Following code snippet is what I am using in order to understand the permission set by the user as in newer android devices permissions can be tweaked for specific app from settings.

I want to alert a user to give permission to avoid app crashing But the following snippet is always returning true for me. What am I doing wrong?

//If authorisation not granted for camera
boolean permission = (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)==PackageManager.PERMISSION_GRANTED);


if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
    //ask for authorisation
    //Manifest.permission.CAMERA
    if (ActivityCompat.shouldShowRequestPermissionRationale(this,
            Manifest.permission.CAMERA)) {
        showExplanation("Permission Needed", "Rationale", Manifest.permission.CAMERA, REQUEST_PERMISSION_CAMERA);
    }
    else
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, REQUEST_PERMISSION_CAMERA);
}
else
    try {
        //releasing camera if it's already in use
        releaseCamera();
        camera = Camera.open(camId);
}catch (Exception e)
{
    e.printStackTrace();
}
Dani M
  • 1,173
  • 1
  • 15
  • 43
Sudip Bhandari
  • 2,165
  • 1
  • 27
  • 26
  • 2
    *But the following snippet is always returning true for me* you have either granted already the permission or have a pre-marshmallow version of android on your device – Blackbelt Nov 28 '16 at 13:25
  • I am using nexus 5 Marshmallow. I tried multiple times setting permission on and off. permission is always returned as true for me however the app crashes (when permission is not given) – Sudip Bhandari Nov 28 '16 at 13:29
  • refer this http://stackoverflow.com/questions/34342816/android-6-0-multiple-permissions – Radhey Nov 28 '16 at 13:32

2 Answers2

1
boolean permission = (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)==PackageManager.PERMISSION_GRANTED);

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
    //ask for authorisation
    //Manifest.permission.CAMERA
    if (ActivityCompat.shouldShowRequestPermissionRationale(this,
            Manifest.permission.READ_PHONE_STATE)) {
        showExplanation("Permission Needed", "Rationale", Manifest.permission.READ_PHONE_STATE, REQUEST_PERMISSION_CAMERA);
    }
    else
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, REQUEST_PERMISSION_CAMERA);
}
else{
    try {
        //releasing camera if it's already in use
        releaseCamera();
        camera = Camera.open(camId);
}catch (Exception e)
{
    e.printStackTrace();
}}   /////// put your else condition in braces
Dani M
  • 1,173
  • 1
  • 15
  • 43
Ak9637
  • 990
  • 6
  • 12
  • Code is useless without explanation – Tim Nov 28 '16 at 13:59
  • @Tim Castelijins i have just edited the posted code,it is pretty self explanatory – Ak9637 Nov 28 '16 at 14:00
  • I can't find what you changed, please tell. And please explain why you changed it – Tim Nov 28 '16 at 14:03
  • ActivityCompat instead of ContextCompat , as only an activity can check for permissions . ty @Tim would make sure next time i provide explanation even for changes :) – Ak9637 Nov 28 '16 at 14:04
  • check https://developer.android.com/reference/android/support/v4/content/ContextCompat.html#checkSelfPermission(android.content.Context, java.lang.String) – Tim Nov 28 '16 at 14:05
  • @Tim i agree,please let me know if there is any difference between ContextCompat and ActivityCompat ..... as the permission model only works for me when i use the latter only – Ak9637 Nov 28 '16 at 14:11
  • depends on the sdk version you use. ContextCompat can be used for 23+ – Tim Nov 28 '16 at 14:14
  • offcorse,the permission model has been introduced from 23+ only, but still ActivityCompat works for me but not ContextCompat,why ? ......can u help me with the reason ? @ Tim – Ak9637 Nov 28 '16 at 14:15
0

I was facing this issue because this feature is available only in api level 23 and above. I was compiling on 22.

Sudip Bhandari
  • 2,165
  • 1
  • 27
  • 26