Skip to content Skip to sidebar Skip to footer

Android - Correct Way To Handle A Dangerous Permission That Must Be Granted For App To Work?

My app requires access to the storage - just to read some OBB data - due to the size of the data we're dealing with. By definition this is classed as a 'dangerous' permission by an

Solution 1:

The crucial bit not yet described in the other answers is after a user has declined to provide the permission, the recommended approach is to show a rationale dialog explaining why your permission is essential. There is a compatibility helper that tracks this internal state:

if (ActivityCompat.shouldShowRequestPermissionRationale(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE)) {

        new AlertDialog.Builder(this)
                .setTitle(R.string.title_my_permission)
                .setMessage(R.string.text_my_permission)
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    publicvoidonClick(DialogInterface dialogInterface, int i) {
                        //Prompt the user once explanation has been shown
                        ActivityCompat.requestPermissions(MainActivity.this,
                                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                                MY_PERMISSIONS_REQUEST_TAG);
                    }
                })
                .create()
                .show();
    } else {
        // probably first time, request permission
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                MY_PERMISSIONS_REQUEST_TAG);
    }

You should note however that the user is always in control and may decide never to grant your app the permission it needs, so you need a good bit of text in as many locales as you can manage to convince them :-)

Worse, they can select "never ask again" in the original request dialog. In that case, ActivityCompat.shouldShowRequestPermissionRationale returns false also. If you really want to, you can warn the user using a third dialog. However, in that case, the user themselves must manually enable the permission inside the Android settings activity. You can help them find this activity using an intent but they must manually check the permission once they have selected "never ask again".

Solution 2:

You can ask for permission when it starts like this:

if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE)) {

        } else {
            ActivityCompat.requestPermissions(getActivity(), newString[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, RC_WRITE_EXTERNAL_STORAGE);
        }
    }

and if permission is not granted a dialog will show to grant permission that you can get the result in :

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode ==  RC_WRITE_EXTERNAL_STORAGE && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        }
    }

Solution 3:

Solution 4:

step 1 :- add this permission in manifiest file

 android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
 android.Manifest.permission.READ_EXTERNAL_STORAGE,

step 2 : ask runtime permission

Stringpermission= android.Manifest.permission.READ_EXTERNAL_STORAGE;
        if (ActivityCompat.checkSelfPermission(MainActivity.this, permission)!= PackageManager.PERMISSION_GRANTED ) {
            ActivityCompat.requestPermissions(MainActivity.this, newString[]
                    {permission}, PERMISSION_CODE);

        }

step 3: handle permsiion result

@Override
 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                   @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSION_CODE) {
    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {


        Toast.makeText(this, "permission_granted_msg", Toast.LENGTH_SHORT).show();

    } else {

        Toast.makeText(this, "permission_not_granted_msg", Toast.LENGTH_SHORT).show();
    }
}

}

Post a Comment for "Android - Correct Way To Handle A Dangerous Permission That Must Be Granted For App To Work?"