1

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;
            }
    }
Awais Aslam
  • 75
  • 1
  • 1
  • 9

3 Answers3

0

There is no need to use that Boolean to handle permission

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    if (checkSelfPermission(Manifest.permission.CAMERA)  != PackageManager.PERMISSION_GRANTED) {
                        requestPermissions(new String[]{Manifest.permission.CAMERA}, 1001);
                    } else {
                       Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, 7);
                    }
                } else {
                    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, 7);
                } 

and in onRequestPermissionsResult

 @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (grantResults != null && grantResults.length > 0) {
            switch (requestCode) {
                case 1001:
                    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                         Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, 7);
                    } else {
                         if (ActivityCompat.shouldShowRequestPermissionRationale(HomeActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                        Toast.makeText(this, "Please allow permission", Toast.LENGTH_SHORT).show();
                    } else {
                        AlertDialog dialog= new AlertDialog.Builder(HomeActivity.this)
                                .setMessage("Apply permission")
                                .setPositiveButton("Open Settings", new DialogInterface.OnClickListener() {

                                    public void onClick(DialogInterface dialog, int whichButton) {
                                        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 );
                                    }

                                })
                                .create();
                        dialog.show();
                    }
                    }
                    break;

            }
        }
    }
Sagar gujarati
  • 152
  • 1
  • 15
  • not helping, i'm handling the case "when app shows request permission dialog box, if user click on don't show again checkbox and deny the permission, the next time user again click on button,it should take user to settings. its working but problem is when user restart the activity, it won't work for the first time, – Awais Aslam Sep 05 '19 at 08:44
0

You can use ActivityCompat#shouldShowRequestPermissionRational on onRequestPermissionsResult

Original answer: Link

fshdn19
  • 374
  • 1
  • 8
0

i solved it by using sharedpreference. i put boolean value into sharedpreference like this:

if (!showRational){
                        boolean isChecked= true;
                        sharedpreferences = getSharedPreferences(mypreference,
                                Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sharedpreferences.edit();
                        editor.putBoolean("isChecked", isChecked);
                        editor.commit();
                    }

and get it in on button Click like this:

 sharedpreferences = getSharedPreferences(mypreference,
                    Context.MODE_PRIVATE);
            boolean checked = sharedpreferences.getBoolean("isChecked", false);
            if (checkPermission())
            {
                Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, 7);
            }
            else if(checked)
            {
                openSetting();
            }else {

                requestPermission();
            }

now when i restart the activity, it won't change the value of boolean variable.

So the conclusion is "if someone wants to use a variable:

-which should be declared and initialize in one method and should be called in another method(out of scope), -(OR) which needs to initialize once in a METHOD and then shouldn't be changed ever, Plus should be available in Any Method.

we will save that variable's value in sharedpreference and get it anywhere we want it.

Awais Aslam
  • 75
  • 1
  • 1
  • 9