How to ask again for permission if it was denied in android

Viewed 6747

How user denies then the dialog box never pops up again, this is the code I use:

ActivityCompat.requestPermissions(MainActivity.this,new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE}, PackageManager.PERMISSION_GRANTED);

Could somebody give me some help please as to how make the dialog pops up again? Thanks in advance.

2 Answers

Ok, turns out android 11 only asks for permission twice, as opposed to previous versions that ask over and over again (this is what I was expecting from the beginning, hence the confusion as to why the function works only two times).

It is best practice that if a user denies a permission that you as the developer should design your program in such a way so that the user should still be able to use your app. Therefore if you can degrade your app if the user denies your request. But if you have made up you mind that you can't do this then do as follows.

Does requesting the permission only happens during a user event e.g. when the user clicks a button, write some text in a edit text etc..

If so why not add an on click implementation to the view (e.g. button, text view) so that every time they click it you check if the user has granted permission and request the permission again if they have not.

Your code should look something like this. Note: this is strongly based on the linked answer you where sent to by @javdromero. I only gave this example because you said you were have trouble stucturing it.

  public class MainActivity extends AppCompatActivity {


// Register the permissions callback, which handles the user's response to the
// system permissions dialog. Save the return value, an instance of
// ActivityResultLauncher, as an instance variable.
private final ActivityResultLauncher<String> requestPermissionLauncher =
        registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
            if (isGranted) {
                // Permission is granted. Continue the action or workflow in your
                // app.

            } else {
                // Explain to the user that the feature is unavailable because the
                // features requires a permission that the user has denied. At the
                // same time, respect the user's decision. Don't link to system
                // settings in an effort to convince the user to change their
                // decision.
            }

        });


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


}

// The permission will be checked as the app starts.
@Override
protected void onStart() {
    super.onStart();

// The permission will also be checked on button click
public void myMethod(View view) {
    checkPermissionRequest();
}

// Called if shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION) is not true 
// or if the yes button is pressed in the alert dialog.
public void makePermissionRequest() {
    requestPermissionLauncher.launch(Manifest.permission.ACCESS_FINE_LOCATION);
}


public void checkPermissionRequest() {
    if (ContextCompat.checkSelfPermission(this,
            android.Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED) {
        // continue running app

    } else if (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)) {
        showAlertDialog();
    } else {
        makePermissionRequest();
    }
}

// is called if the permission is not given.
public void showAlertDialog() {
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setMessage("This app needs you to allow this 
     permission in order to function.Will you allow it");
    alertDialogBuilder.setPositiveButton("Yes",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    makePermissionRequest();
                }
            });

    alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });

    AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();

}


 }

The alert dialog will only show if the shouldShowRequestPermissionRationale method returns true, the user has not allowed the permission, denied or denied it by selecting Deny and don't ask again. If the user selected Deny and don't ask again, only they can change it in the app settings, what you can do as the developer is direct them to the settings by using an intent. Also according to the android docs "Starting in Android 11 (API level 30), if the user taps Deny for a specific permission more than once during your app's lifetime of installation on a device, the user doesn't see the system permissions dialog if your app requests that permission again. The user's action implies "don't ask again." Therefore if you are testing on a device that use android 11 this is what is going to happen and you may have to direct the user to the settings of the app in this case as well.

You can also close your app if the user denies it but I won't really recommend this.

for more on requesting permission go to the android documentation.

here: https://developer.android.com/training/permissions/requesting#handle-denial

Related