// Here, thisActivity is the current activity if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation? if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, Manifest.permission.READ_CONTACTS)) {
// Show an expanation to the user *asynchronously* -- don't block // this thread waiting for the user's response! After the user // sees the explanation, try again to request the permission. //异步显示对用户的扩展* * *不阻止 //这个线程等待用户的响应!用户后 //参考解释,请再次请求许可。 } else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(thisActivity, new String[]{Manifest.permission.READ_CONTACTS}, MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an // app-defined int constant. The callback method gets the // result of the request. } }
@Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case MY_PERMISSIONS_REQUEST_READ_CONTACTS: { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the // contacts-related task you need to do.
} else {
// permission denied, boo! Disable the // functionality that depends on this permission. } return; }
// other 'case' lines to check for other // permissions this app might request } }
<!-- Note that all required permissions are declared here in the Android manifest. On Android M and above, use of these permissions is only requested at run time. --> <uses-permission android:name="android.permission.CAMERA" />
<!-- The following permissions are only requested if the device is on M or above. On older platforms these permissions are not requested and will not be available. --> <uses-permission-sdk-23 android:name="android.permission.READ_CONTACTS" /> <uses-permission-sdk-23 android:name="android.permission.WRITE_CONTACTS" />
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // Camera permission has been granted, preview can be displayed Log.i(TAG, "CAMERA permission has now been granted. Showing preview."); Snackbar.make(mLayout, R.string.permision_available_camera, Snackbar.LENGTH_SHORT).show();
/** * Called when the 'show camera' button is clicked. * Callback is defined in resource layout definition. */ public void showContacts(View v) { Log.i(TAG, "Show contacts button pressed. Checking permissions.");
// Verify that all required contact permissions have been granted. if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CONTACTS) != PackageManager.PERMISSION_GRANTED) { // Contacts permissions have not been granted. Log.i(TAG, "Contact permissions has NOT been granted. Requesting permissions."); requestContactsPermissions();
} else {
// Contact permissions have been granted. Show the contacts fragment. Log.i(TAG, "Contact permissions have already been granted. Displaying contact details."); showContactDetails(); } }
/** * Requests the Contacts permissions. * If the permission has been denied previously, a SnackBar will prompt the user to grant the * permission, otherwise it is requested directly. */ private void requestContactsPermissions() { // BEGIN_INCLUDE(contacts_permission_request) if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_CONTACTS) || ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_CONTACTS)) {
// Provide an additional rationale to the user if the permission was not granted // and the user would benefit from additional context for the use of the permission. // For example, if the request has been denied previously. Log.i(TAG, "Displaying contacts permission rationale to provide additional context.");
// Display a SnackBar with an explanation and a button to trigger the request. Snackbar.make(mLayout, R.string.permission_contacts_rationale, Snackbar.LENGTH_INDEFINITE) .setAction(R.string.ok, new View.OnClickListener() { @Override public void onClick(View view) { ActivityCompat .requestPermissions(MainActivity.this, PERMISSIONS_CONTACT, REQUEST_CONTACTS); } }) .show(); } else { // Contact permissions have not been granted yet. Request them directly. ActivityCompat.requestPermissions(this, PERMISSIONS_CONTACT, REQUEST_CONTACTS); } // END_INCLUDE(contacts_permission_request) }
/** * Callback received when a permissions request has been completed. */ @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == REQUEST_CAMERA) { // BEGIN_INCLUDE(permission_result) // Received permission result for camera permission. Log.i(TAG, "Received response for Camera permission request.");
// Check if the only required permission has been granted if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // Camera permission has been granted, preview can be displayed Log.i(TAG, "CAMERA permission has now been granted. Showing preview."); Snackbar.make(mLayout, R.string.permision_available_camera, Snackbar.LENGTH_SHORT).show(); } else { Log.i(TAG, "CAMERA permission was NOT granted."); Snackbar.make(mLayout, R.string.permissions_not_granted, Snackbar.LENGTH_SHORT).show();
} // END_INCLUDE(permission_result)
} else if (requestCode == REQUEST_CONTACTS) { Log.i(TAG, "Received response for contact permissions request.");
// We have requested multiple permissions for contacts, so all of them need to be // checked. if (PermissionUtil.verifyPermissions(grantResults)) { // All required permissions have been granted, display contacts fragment. Snackbar.make(mLayout, R.string.permision_available_contacts, Snackbar.LENGTH_SHORT) .show(); } else { Log.i(TAG, "Contacts permissions were NOT granted."); Snackbar.make(mLayout, R.string.permissions_not_granted, Snackbar.LENGTH_SHORT) .show(); }