Handling Marshmallow4:15 with Ben Deitch
In this video we'll make the app work on devices running API 23 or newer!
Our code is most of the way there, but 0:00 we still need to handle devices running newer versions of Android. 0:02 Let's start in the onClick method by checking if the user's device 0:05 is running Marshmallow or higher. 0:08 So if Build.VERSION.STK_INT is greater than or 0:10 equal to Build.VERSION_CODES.M for Marshmallow, 0:15 and then let's add an else statement around our makeCall call. 0:21 If the user's on an earlier version of Android we can just call makeCall 0:33 directly. 0:36 Back on the first part of the if statement, 0:37 we need to check if we already have the call phone permission. 0:39 If we do, then we're free to make our call. 0:43 But if we don't, then we'll need to request that permission and 0:46 handle the result. 0:49 To check if we already have that permission, 0:50 let's type if(checkSelfPermission, and let's pass in that permission. 0:54 So Manifest and we want the Android version, .permission.CALL_PHONE. 1:01 And we're gonna check that this is not equal to 1:08 PackageManager.PERMISSION_GRANTED. 1:13 So if this is true, then we don't have the permission. 1:20 And if this is false, then we do have the permission and we can make our call. 1:24 So let's add an else statement, And then call the makeCall function. 1:28 Finally, if we don't have permission, then we need to request it. 1:35 So inside the if statement, let's add a call to requestPermissions. 1:40 requestPermissions takes in a string array of permissions to request and 1:46 a request code. 1:50 So let's pass in a new string array containing the call phone permission, 1:52 new String array, with the, CALL_PHONE permission. 1:58 And for the second parameter, let's pass in a constant that doesn't yet exist. 2:07 Named PERMISSIONS_REQUEST_CODE. 2:10 Then let's use Alt+Enter to make that exist in MainActivity and 2:13 let's set it equal to some integer. 2:21 I'll pick 11. 2:26 Next, let's put our cursor down here and 2:29 use Ctrl+O to override onRequestPermissionResult. 2:32 And we can get rid of the call to super. 2:38 Inside this method, let's first check that the request code passed in 2:40 is equal to our permissions request code. 2:44 If request code == PERMISSIONS_REQUEST_CODE. 2:47 And if it does, 2:51 then we can use the grantResults array to see if our permission was granted. 2:53 So let's first make sure that the grantResults array isn't empty. 2:59 If grantResults.length > 0. 3:03 And then, since we only requested one permission, let's check at the first entry 3:09 in the grantResults array is equal to PackageManager.PERMISSION_GRANTED. 3:13 So && grantResults == PackageManager.PERMISSION_GRANTED. 3:20 And if that's all true, then we've got permission, and we can make our call. 3:31 Now let's run the app and see what happens, 3:37 and we're gonna run it on the API 23 device, which is Marshmallow. 3:39 And if we click on the CALL button, we get the option to either allow or 3:50 deny this permission, and if we click DENY, nothing happens. 3:56 And it will re-prompt us the next time we click the CALL button, and 4:00 once we click ALLOW, we're finally able to make our call. 4:03 Nice, but before I let you go, there's one more thing you need to know 4:07 when dealing with permissions, and we'll see what that is in the next video. 4:11
You need to sign up for Treehouse in order to download course files.Sign up