5454 * {@link Activity#requestPermissions(String[], int)} and the return value checked in {@link
5555 * Activity#onRequestPermissionsResult(int, String[], int[])}.
5656 * <p>
57+ * Before requesting permissions, {@link Activity#shouldShowRequestPermissionRationale(String)}
58+ * should be called to provide the user with additional context for the use of permissions if they
59+ * have been denied previously.
60+ * <p>
5761 * If this sample is executed on a device running a platform version below M, all permissions
5862 * declared
5963 * in the Android manifest file are always granted at install time and cannot be requested at run
@@ -102,14 +106,26 @@ public void showCamera(View view) {
102106 // BEGIN_INCLUDE(camera_permission)
103107 // Check if the Camera permission is already available.
104108 if (PermissionUtil .hasSelfPermission (this , Manifest .permission .CAMERA )) {
109+ // Camera permissions is already available, show the camera preview.
105110 Log .i (TAG ,
106111 "CAMERA permission has already been granted. Displaying camera preview." );
107- // Camera permissions is already available, show the camera preview.
108112 showCameraPreview ();
109113 } else {
114+ // Camera permission has not been granted.
110115 Log .i (TAG , "CAMERA permission has NOT been granted. Requesting permission." );
111- // Camera permission has not been granted. Request it.
112- requestPermissions (new String []{Manifest .permission .CAMERA }, REQUEST_CAMERA );
116+
117+ // Provide an additional rationale to the user if the permission was not granted
118+ // and the user would benefit from additional context for the use of the permission.
119+ if (shouldShowRequestPermissionRationale (Manifest .permission .CAMERA )) {
120+ Log .i (TAG ,
121+ "Displaying camera permission rationale to provide additional context." );
122+ Toast .makeText (this , R .string .permission_camera_rationale , Toast .LENGTH_SHORT )
123+ .show ();
124+ }
125+
126+ // Request Camera permission
127+ requestPermissions (new String []{Manifest .permission .CAMERA },
128+ REQUEST_CAMERA );
113129 }
114130 // END_INCLUDE(camera_permission)
115131
@@ -128,7 +144,18 @@ public void showContacts(View v) {
128144 // Contact permissions have been granted. Show the contacts fragment.
129145 showContactDetails ();
130146 } else {
147+ // Contacts permissions have not been granted.
131148 Log .i (TAG , "Contact permissions has NOT been granted. Requesting permission." );
149+
150+ // Provide an additional rationale to the user if the permission was not granted
151+ // and the user would benefit from additional context for the use of the permission.
152+ if (shouldShowRequestPermissionRationale (Manifest .permission .CAMERA )) {
153+ Log .i (TAG ,
154+ "Displaying contacts permission rationale to provide additional context." );
155+ Toast .makeText (this , R .string .permission_contacts_rationale , Toast .LENGTH_SHORT )
156+ .show ();
157+ }
158+
132159 // contact permissions has not been granted (read and write contacts). Request them.
133160 requestPermissions (PERMISSIONS_CONTACT , REQUEST_CONTACTS );
134161 }
0 commit comments