This workshop will be retired on May 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Android Permissions!
You have completed Android Permissions!
Preview
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
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
The app works just fine,
what else could we want?
0:00
Well sometimes a user
will deny a permission
0:03
because they don't know
why it's necessary.
0:06
And in these cases it's
up to us the developers
0:08
to help the user understand
why we need that permission.
0:12
To make this easier for us,
0:15
Android contains a method which returns
true if the user has denied a permission.
0:16
Let's see how that works.
0:22
Back in the on click method above
our call to request permissions,
0:23
let's add an if statement and
then for the condition,
0:29
let's type should show request
permission rationale and
0:33
then let's pass in our permission
manifest dot permission dot call phone.
0:38
If this is true it means the user
has already denied our request.
0:45
And we should probably tell
them why we need the permission
0:49
before requesting it again.
0:52
On the other hand if this is false,
0:54
then we should just request
the permission normally.
0:56
So let's add an else block around
our call to request permissions.
0:59
Lastly inside the if statement,
1:02
we just need to explain to our users,
why we need the permission.
1:09
To do this let's use an alert dialog, I
put one down below in the teacher's notes.
1:13
It's just a simple dialog that tells
the user why we need the permission and
1:18
ask them if they'd be willing to grant it.
1:22
I'm gonna choose alt enter to
import the missing pieces.
1:26
And all we've got to do to finish the
dialog is fill in the on click methods.
1:31
If a user clicks on the positive button
1:35
then they're willing to
grant the permission.
1:38
So for the positive buttons on
click method, let's copy and
1:40
paste in our call to request permissions.
1:43
Otherwise if they click the negative
button, there's not much we can do so
1:58
let's just toast a sad face.
2:03
Nice, now before we run the app again
make sure to first uninstall it from your
2:15
device so
it won't already have the permission.
2:19
Once that's done, let's run the app and
2:29
then click the call button and
choose to deny the permission.
2:33
Then if we click the button again
We get our alert dialog and
2:42
clicking no thanks will
give us a sad face.
2:47
But if we try to call again and
2:51
instead click yes then we get another
chance to allow the permission.
2:53
And if we allow it this time then
we're making our call, great work.
2:58
Using permissions definitely got a little
harder with Android marshmallow but
3:06
that's no excuse not to implement them.
3:09
They're fairly easy to use and they
provide a much better user experience.
3:12
So get out there and
3:16
start updating your apps to use the new
permissions model until next time.
3:17
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up