1 00:00:00,210 --> 00:00:03,480 The app works just fine, what else could we want? 2 00:00:03,480 --> 00:00:06,290 Well sometimes a user will deny a permission 3 00:00:06,290 --> 00:00:08,790 because they don't know why it's necessary. 4 00:00:08,790 --> 00:00:12,080 And in these cases it's up to us the developers 5 00:00:12,080 --> 00:00:15,130 to help the user understand why we need that permission. 6 00:00:15,130 --> 00:00:16,750 To make this easier for us, 7 00:00:16,750 --> 00:00:22,120 Android contains a method which returns true if the user has denied a permission. 8 00:00:22,120 --> 00:00:23,450 Let's see how that works. 9 00:00:23,450 --> 00:00:26,980 Back in the on click method above our call to request permissions, 10 00:00:29,540 --> 00:00:33,480 let's add an if statement and then for the condition, 11 00:00:33,480 --> 00:00:38,160 let's type should show request permission rationale and 12 00:00:38,160 --> 00:00:42,880 then let's pass in our permission manifest dot permission dot call phone. 13 00:00:45,670 --> 00:00:49,960 If this is true it means the user has already denied our request. 14 00:00:49,960 --> 00:00:52,690 And we should probably tell them why we need the permission 15 00:00:52,690 --> 00:00:54,650 before requesting it again. 16 00:00:54,650 --> 00:00:56,510 On the other hand if this is false, 17 00:00:56,510 --> 00:00:59,630 then we should just request the permission normally. 18 00:00:59,630 --> 00:01:02,625 So let's add an else block around our call to request permissions. 19 00:01:02,625 --> 00:01:09,550 Lastly inside the if statement, 20 00:01:09,550 --> 00:01:13,500 we just need to explain to our users, why we need the permission. 21 00:01:13,500 --> 00:01:18,530 To do this let's use an alert dialog, I put one down below in the teacher's notes. 22 00:01:18,530 --> 00:01:22,150 It's just a simple dialog that tells the user why we need the permission and 23 00:01:22,150 --> 00:01:23,770 ask them if they'd be willing to grant it. 24 00:01:26,550 --> 00:01:28,974 I'm gonna choose alt enter to import the missing pieces. 25 00:01:31,399 --> 00:01:35,900 And all we've got to do to finish the dialog is fill in the on click methods. 26 00:01:35,900 --> 00:01:38,420 If a user clicks on the positive button 27 00:01:38,420 --> 00:01:40,880 then they're willing to grant the permission. 28 00:01:40,880 --> 00:01:43,638 So for the positive buttons on click method, let's copy and 29 00:01:43,638 --> 00:01:45,595 paste in our call to request permissions. 30 00:01:58,937 --> 00:02:03,730 Otherwise if they click the negative button, there's not much we can do so 31 00:02:03,730 --> 00:02:05,643 let's just toast a sad face. 32 00:02:15,614 --> 00:02:19,711 Nice, now before we run the app again make sure to first uninstall it from your 33 00:02:19,711 --> 00:02:22,373 device so it won't already have the permission. 34 00:02:29,127 --> 00:02:33,913 Once that's done, let's run the app and 35 00:02:33,913 --> 00:02:41,040 then click the call button and choose to deny the permission. 36 00:02:42,210 --> 00:02:47,829 Then if we click the button again We get our alert dialog and 37 00:02:47,829 --> 00:02:51,260 clicking no thanks will give us a sad face. 38 00:02:51,260 --> 00:02:53,800 But if we try to call again and 39 00:02:53,800 --> 00:02:58,270 instead click yes then we get another chance to allow the permission. 40 00:02:58,270 --> 00:03:06,017 And if we allow it this time then we're making our call, great work. 41 00:03:06,017 --> 00:03:09,914 Using permissions definitely got a little harder with Android marshmallow but 42 00:03:09,914 --> 00:03:12,500 that's no excuse not to implement them. 43 00:03:12,500 --> 00:03:16,840 They're fairly easy to use and they provide a much better user experience. 44 00:03:16,840 --> 00:03:17,790 So get out there and 45 00:03:17,790 --> 00:03:21,660 start updating your apps to use the new permissions model until next time.