Step 0: Add androidx.exercise dependency
First, add the beneath dependency within the app-level construct.gradle
with its model as not less than 1.2.0:
implementation "androidx.exercise:exercise:1.2.0"
Step 1: Declare the permission within the AndroidManifest.xml
file.
Assume that we’re asking permission for studying exterior storage
Step:2 Declare an exercise end result contract for the permission
On the prime of your exercise(contained in the exercise however outdoors of any perform/methodology), create a variable for the exercise end result contract like this.
Right here, ActivityResultContracts.RequestPermission()
is a pre-defined contract for requesting a single permission. And, the block is a callback perform that receives a boolean worth(we renamed that as isPermissionAccepted
) which tells us whether or not the permission is accepted or not.
Right here within the above code, if the permission is accepted then we present a toast with the “Permission is accepted” message else if it’s not accepted, we present a toast with “Permission is declined” message.
However, wait, wait, wait..…. Above it was simply the variable declaration. We have to additional name this variable to indicate the permission dialog to the consumer. It’s proven within the subsequent step.
Step:3 Launching the permission asking dialog
Let’s assume we now have a button with askSinglePermission
its XML id. On click on of that button, we wish to present the permission asking dialog to the consumer.
Within the code above, the parameter contained in the launch()
methodology is referring to the permission we declared within the AndroidManifest.xml
file in step 1.
Right here, on clicking on the button, the launch()
methodology might be executed and the consumer will have the ability to see the permission dialog like this:
As quickly as we click on on the Enable button
, the permission is accepted and the callback of the registerForActivityResult()
methodology (contained in the readExternalPermissionContract
variable declared in step 2) might be known as with the boolean parameter as true
.
Now we’ll see the toast from the if block of the callback.
Notice: As soon as this permission is accepted, the dialog won’t be proven to the consumer additional even in case you name the launch()
methodology by clicking the askSinglePermission
button any variety of occasions. However the registerForActivityResult()
methodology’s callback might be known as on each click on occasion(on launch()
methodology’s execution) with the boolean parameter as true
.
If on clicking on the button the consumer declines the permission, the registerForActivityResult()
methodology’s callback declared in step 2 might be known as with the boolean parameter as false and the else a part of the callback might be executed.
However, if the consumer once more clicks on the askSinglePermission
button then launch()
methodology will once more be known as however as final time the permission was declined, this time the dialog will seem once more.
We are able to see the beneath instance:
Within the above instance, we’re denying the permission for the primary time, so “Permission is declined” toast is proven. However, once more we clicked on the askSinglePermission
button , so the dialog appeared once more, and this time we accepted the permission.
As soon as the permission is accepted, the dialog won’t seem additional on clicking the askSinglePermission
button any variety of occasions. However the registerForActivityResult()
methodology’s callback might be known as on each click on occasion(on launch()
methodology’s execution) with the boolean parameter as true
.