i have a camera permission button,on button click listener i have 2 main following conditions. 1:if permission granted simply go to camera activity... 2:else check 2 further conditions(1:i check(using boolean variable) if ""don't ask again for permission"" checked in dialog box then go to settings....2:else request permission).
And in onRequestPermissionsResult method i checked if user grant permission then go to camera activity... else check if user checked ""don't ask again for permission"" set true that boolean variable else show permission denied.
Everything is working properly but when i restart the activity it sets that boolean variable as false(now if user might have checked ""don't ask again for permission"" last time) and it should take user to the settings but it won't at first click, after one click(when that boolean variable set to true)it will work. SO my question is how i can set that boolean variable final(so it won't change on activity restart) in onRequestPermissionsResult method and use it in onCreate method(out of the scope).
here is button onclick listener code in onCreate.
if (checkPermission())
{
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 7);
// Toast.makeText(GalleryImageSelection.this,"CAMERA permission allows us to Access CAMERA app", Toast.LENGTH_LONG).show();
}
else {
if (test){ //test is that boolean variable
Uri packageUri = Uri.fromParts( "package", getApplicationContext().getPackageName(), null );
Intent intent = new Intent();
intent.setAction( Settings.ACTION_APPLICATION_DETAILS_SETTINGS );
intent.setData( packageUri );
intent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
startActivity( intent );
}else {
ActivityCompat.requestPermissions(GalleryImageSelection.this,new String[]{
Manifest.permission.CAMERA}, RequestPermissionCode);
} }
here is onRequestPermissionsResult method code
@Override
public void onRequestPermissionsResult(int RC, String per[], int[] PResult) {
switch (RC) {
case RequestPermissionCode:
if (PResult[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(GalleryImageSelection.this, "Permission Granted, Now your application can access CAMERA.", Toast.LENGTH_LONG).show();
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 7);
} else {
// for (int i = 0, len = per.length; i < len; i++) {
// String permission = per[i];
if (PResult[0] == PackageManager.PERMISSION_DENIED){
boolean showRational =shouldShowRequestPermissionRationale(per[0]);
if (!showRational){
test = true;//test is declared above onCreate for
//scope purpose
}
else if (CAMERA.equals(per[0])) {
Toast.makeText(GalleryImageSelection.this, "Permission Denied, your application cannot access CAMERA.", Toast.LENGTH_LONG).show();
}
}
}
break;
}
}