Android Handling Permission Deny With Don't Ask Again In Java

In the previous example we have seen how to make runtime permission request and handle once user action i.e allow and deny ,when you make request for permission system will generate popup for user action .


When you make request for permission there will be 3 actions user's can perform .

  • Action 1 :  Allow 
  • Action 2 :  Just Deny ( deny without selecting the check-box "don't ask again")
  • Action 3 :  Force Deny ( deny with selecting the check-box "don't ask again")

Action 1 and 2 we have seen in previous example , In this tutorial we will handling Action 3 .

When you deny permission by selecting the check-box "don't ask again" After that when you make subsequent request again for the same permission Nothing will happen ( i.e The system will not show "Allow Deny" popup again )  .

Lets See Sample Example .

  UseCase  
Scenerio
1
  Request Permission > Pop-up > allow > (TextView Updated With Permission Granted).

 2
   Request Permission > Pop-up > deny > (TextView Updated With Permission Denied).

 3

    Request Permission > Pop-up > deny with "don't ask again" selected >
    (TextView Updated With Permission Denied and Show Button for visit settings).
 
   Now on Click of "visit settings" you will navigate to app settings 
   once you grant permission and resume back to app  inside onResume
   again we will check if permission update the UI  (i.e TextView Updated 
   with Permission  Granted and hide Button for visit settings ).



Use Case 1 ( Permission Granted )



Use Case 2 ( Permission Denied )
 



Use Case 3 ( Pemission Deny By Selecting Don't Ask Again )



MainActivity


file : MainActivity.java
package com.tutorialsbuzz.permissiondeny;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    public static final int REQUEST_WRITE_PERMISSION = 100;
    private Button allowPermissionViaSettingsBtn;
    private TextView label;

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

    private void initViews() {
        label = findViewById(R.id.label);

        Button requestPermissionBtn = findViewById(R.id.btn);
        requestPermissionBtn.setOnClickListener(this);

        allowPermissionViaSettingsBtn = findViewById(R.id.allow_permission);
        allowPermissionViaSettingsBtn.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (checkPermissionForReadExtertalStorage()) {
            label.setText(R.string.permission_granted);
            allowPermissionViaSettingsBtn.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn:

                if (checkPermissionForReadExtertalStorage()) {
                    label.setText(R.string.permission_granted);
                } else {
                    //Make Request
                    requestPermission();
                }

                break;

            case R.id.allow_permission:

                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                Uri uri = Uri.fromParts("package", getPackageName(), null);
                intent.setData(uri);
                startActivity(intent);

                break;
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (grantResults.length > 0) {
            if (requestCode == REQUEST_WRITE_PERMISSION)
                if (grantResults[0]
                        == PackageManager.PERMISSION_GRANTED) {
                    // permission granted
                    label.setText(R.string.permission_granted);
                    allowPermissionViaSettingsBtn.setVisibility(View.GONE);

                } else {
                    // permission denied
                    label.setText(R.string.permission_denied);
                    // Check wheather checked dont ask again
                    checkUserRequestedDontAskAgain();
                }
        }
    }

    public boolean checkPermissionForReadExtertalStorage() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            return ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
                    || ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
        }
        return false;
    }

    private void requestPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    REQUEST_WRITE_PERMISSION);
        }
    }

    private void checkUserRequestedDontAskAgain() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            boolean rationalFalgREAD = shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            boolean rationalFalgWRITE = shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE);

            if (!rationalFalgREAD && !rationalFalgWRITE) {
                label.setText(R.string.permission_denied_forcefully);
                allowPermissionViaSettingsBtn.setVisibility(View.VISIBLE);
            }
        }
    }

}



No comments:

Post a Comment