Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
In this video we'll see how we can handle a user who denies our permission request!
AlertDialog
new AlertDialog.Builder(MainActivity.this)
.setTitle("Call Permission")
.setMessage("Hi there! We can't call anyone without the call permission, could you please grant it?")
.setPositiveButton("Yep", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setNegativeButton("No thanks", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
}).show();
Related Links
-
0:00
The app works just fine, what else could we want?
-
0:03
Well sometimes a user will deny a permission
-
0:06
because they don't know why it's necessary.
-
0:08
And in these cases it's up to us the developers
-
0:12
to help the user understand why we need that permission.
-
0:15
To make this easier for us,
-
0:16
Android contains a method which returns true if the user has denied a permission.
-
0:22
Let's see how that works.
-
0:23
Back in the on click method above our call to request permissions,
-
0:29
let's add an if statement and then for the condition,
-
0:33
let's type should show request permission rationale and
-
0:38
then let's pass in our permission manifest dot permission dot call phone.
-
0:45
If this is true it means the user has already denied our request.
-
0:49
And we should probably tell them why we need the permission
-
0:52
before requesting it again.
-
0:54
On the other hand if this is false,
-
0:56
then we should just request the permission normally.
-
0:59
So let's add an else block around our call to request permissions.
-
1:02
Lastly inside the if statement,
-
1:09
we just need to explain to our users, why we need the permission.
-
1:13
To do this let's use an alert dialog, I put one down below in the teacher's notes.
-
1:18
It's just a simple dialog that tells the user why we need the permission and
-
1:22
ask them if they'd be willing to grant it.
-
1:26
I'm gonna choose alt enter to import the missing pieces.
-
1:31
And all we've got to do to finish the dialog is fill in the on click methods.
-
1:35
If a user clicks on the positive button
-
1:38
then they're willing to grant the permission.
-
1:40
So for the positive buttons on click method, let's copy and
-
1:43
paste in our call to request permissions.
-
1:58
Otherwise if they click the negative button, there's not much we can do so
-
2:03
let's just toast a sad face.
-
2:15
Nice, now before we run the app again make sure to first uninstall it from your
-
2:19
device so it won't already have the permission.
-
2:29
Once that's done, let's run the app and
-
2:33
then click the call button and choose to deny the permission.
-
2:42
Then if we click the button again We get our alert dialog and
-
2:47
clicking no thanks will give us a sad face.
-
2:51
But if we try to call again and
-
2:53
instead click yes then we get another chance to allow the permission.
-
2:58
And if we allow it this time then we're making our call, great work.
-
3:06
Using permissions definitely got a little harder with Android marshmallow but
-
3:09
that's no excuse not to implement them.
-
3:12
They're fairly easy to use and they provide a much better user experience.
-
3:16
So get out there and
-
3:17
start updating your apps to use the new permissions model until next time.
You need to sign up for Treehouse in order to download course files.
Sign up