user9329083
user9329083

Reputation:

How Does Permission get asked Again?

I am seeking to copy my SQLite database to "external" storage as a means of backing up my database file. From there the user can grab the .db file and move to a place deemed safe.

In following the Android Developer documentation on getting permission to WRITE_EXTERNAL_STORAGE, I have entered the code shown in verbatim and I stepped through it with the debugger. (I changed the READ_CONTACTS to WRITE_EXTERNAL_STORAGE)

It will first checkSelfPermission and skip shouldShowRequestPermissionRationale going to requestPermissions. I'm then given an alert popup asking for my permission.

If I deny it, it will then, on the second run through, go to checkSelfPermission again and then go to shouldShowRequestPermissionRationale but skip requestPermissions and terminate not asking for permission again as the Android documentation says it should.

My Questions:

  1. Am I supposed to add a call to requestPermissions in the shouldShowRequestPermissionRationale if() block to get it to ask again? Or is there another way?
  2. If I want to explain the reason or rational for why the permission is needed, am I supposed to implement my own AlertDialog box? Or is there a set method to the ask again AlertBox I haven't discovered yet to provide the system with my explanation?

So far everything I have done in the writing of this app has been a learning experience. I'm having a great time.

EDIT: I do have the

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"  />

set in my Manafest.

Upvotes: 1

Views: 1681

Answers (3)

user6490462
user6490462

Reputation:

How does permission got asked again?

Short answer: is by asking it again.

Some explanation: You can provide an alert dialog or something to demonstrate why you need this permission, also it's not required to do that every time because the user can know which permission required depend on action, however, there is a method I used to check for permissions and it's look like:

public class CheckPermissions {


public static boolean hasPermission(int PERMISSION_REQUEST, String permission, Context context) {
    if (ContextCompat.checkSelfPermission(context,
            permission)
            != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context,
                permission)&&
                ContextCompat.checkSelfPermission(context,
                        permission)
                        != PackageManager.PERMISSION_GRANTED){

            return false;

        } else {

            ActivityCompat.requestPermissions((Activity) context,
                    new String[]{permission},
                    PERMISSION_REQUEST);

        }

        return false;
    } else {
        return true;


       }
    }
}

So you can check every time you need.

 if (CheckPermissions.hasPermission(PERMISSION_REQUEST, Manifest.permission.YOUR_PERMISSION, context)){
// do some thing
}

Upvotes: 1

VSB
VSB

Reputation: 10415

1- If by on the second run through you mean that user is denied your request for permission a few seconds ago, based on your your implementation you do not need to call requestPermissions inside shouldShowRequestPermissionRationale if() block directly, because user rejected your request once and you should respect his/her decision. If shouldShowRequestPermissionRationale returned true value, you can simply show some dialog/toast or a snackbar to explain user why you need this permission and you can provide some methods to ask for permission again inside that dialog/snackbar, for example a grant access button to call requestPermissions again but since user clicked it is based on user request and is not fired automatically inside if() block. If shouldShowRequestPermissionRationale returned false, you simply change the process of your application such that it can goes on without that permission for example stop or show a disabled view to let user know because of his/her decision about permission this function is not available anymore.

2- You should show the user reason for permission request on your customized user interface view based on design of your application. You may use alert dialogs or snackbars. Alert dialog are common however since they block other UI elements they are not suggested from user experience perspective.

You can have a look at samples source provided by google here and here. I suggest to have a look at this question and its answer and its first comment which might give you a good understanding.

Upvotes: 0

Kelevandos
Kelevandos

Reputation: 7082

In the rationale callback, you should call the same logic as before, just adding a reason the user should really, really allow this permission :-) Show a Toast or maybe an AlertDialog and then fire the permission request again. If you go for the dialog, request permission once the user dismisses the Dialog.

If the user denies again, you should not request again. The user may decide they want to enable it after all through Settings, but in general they think you do not deserve the permission :-(

Upvotes: 0

Related Questions