/**
* Requests given permission.
* If the permission has been denied previously, a Dialog will prompt the user to grant the
* permission, otherwise it is requested directly.
*/
protected void requestPermission(final String permission, String rationale, final int requestCode) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
showAlertDialog(getString(R.string.permission_title_rationale), rationale,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(BaseActivity.this,
new String[]{permission}, requestCode);
}
}, getString(R.string.label_ok), null, getString(R.string.label_cancel));
} else {
ActivityCompat.requestPermissions(this, new String[]{permission}, requestCode);
}
}
/**
* This method shows dialog with given title & message.
* Also there is an option to pass onClickListener for positive & negative button.
*
* @param title - dialog title
* @param message - dialog message
* @param onPositiveButtonClickListener - listener for positive button
* @param positiveText - positive button text
* @param onNegativeButtonClickListener - listener for negative button
* @param negativeText - negative button text
*/
protected void showAlertDialog(@Nullable String title, @Nullable String message,
@Nullable DialogInterface.OnClickListener onPositiveButtonClickListener,
@NonNull String positiveText,
@Nullable DialogInterface.OnClickListener onNegativeButtonClickListener,
@NonNull String negativeText) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(title);
builder.setMessage(message);
builder.setPositiveButton(positiveText, onPositiveButtonClickListener);
builder.setNegativeButton(negativeText, onNegativeButtonClickListener);
mAlertDialog = builder.show();
}
//在BaseActivity中声明此方法
//不安全的权限
<table style="font-size: 14px; margin: 0.5em 1em 1em 0px; border-collapse: collapse; border-spacing: 0px; border: 0px; width: 939px; color: rgba(0, 0, 0, 0.682353); font-family: Roboto, sans-serif; line-height: 24px; background-color: rgb(247, 247, 247);"><tbody><tr><th scope="col" style="padding: 4px 12px; vertical-align: top; color: rgb(255, 255, 255); border: 1px solid rgb(221, 221, 221); font-weight: normal; background-color: rgb(153, 153, 153);">Permission Group</th><th scope="col" style="padding: 4px 12px; vertical-align: top; color: rgb(255, 255, 255); border: 1px solid rgb(221, 221, 221); font-weight: normal; background-color: rgb(153, 153, 153);">Permissions</th></tr><tr><td style="padding: 4px 12px; vertical-align: top; border: 1px solid rgb(221, 221, 221); background-color: inherit;"><code style="font-size: 13px; color: rgb(0, 102, 0); font-stretch: normal; line-height: 18px; font-family: Consolas, "Liberation Mono", Menlo, Monaco, Courier, monospace; -webkit-font-smoothing: subpixel-antialiased; margin-bottom: 0px;"><a target=_blank href="https://developer.android.com/reference/android/Manifest.permission_group.html#CALENDAR" style="color: rgb(3, 155, 229); text-decoration: none; margin-bottom: 0px;">CALENDAR</a></code></td><td style="padding: 4px 12px; vertical-align: top; border: 1px solid rgb(221, 221, 221); background-color: inherit;"><ul style="margin: 0px 0px 15px 20px; padding: 0px;"><li style="margin: 0px;"><code style="font-size: 13px; color: rgb(0, 102, 0); font-stretch: normal; line-height: 18px; font-family: Consolas, "Liberation Mono", Menlo, Monaco, Courier, monospace; -webkit-font-smoothing: subpixel-antialiased; margin-bottom: 0px;"><a target=_blank href="https://developer.android.com/reference/android/Manifest.permission.html#READ_CALENDAR" style="color: rgb(3, 155, 229); text-decoration: none; margin-bottom: 0px;">READ_CALENDAR</a></code></li></ul><ul style="margin: 0px 0px 0px 20px; padding: 0px;"><li style="margin: 0px;"><code style="font-size: 13px; color: rgb(0, 102, 0); font-stretch: normal; line-height: 18px; font-family: Consolas, "Liberation Mono", Menlo, Monaco, Courier, monospace; -webkit-font-smoothing: subpixel-antialiased; margin-bottom: 0px;"><a target=_blank href="https://developer.android.com/reference/android/Manifest.permission.html#WRITE_CALENDAR" style="color: rgb(3, 155, 229); text-decoration: none; margin-bottom: 0px;">WRITE_CALENDAR</a></code></li></ul></td></tr><tr><td style="padding: 4px 12px; vertical-align: top; border: 1px solid rgb(221, 221, 221); background-color: inherit;"><code style="font-size: 13px; color: rgb(0, 102, 0); font-stretch: normal; line-height: 18px; font-family: Consolas, "Liberation Mono", Menlo, Monaco, Courier, monospace; -webkit-font-smoothing: subpixel-antialiased; margin-bottom: 0px;"><a target=_blank href="https://developer.android.com/reference/android/Manifest.permission_group.html#CAMERA" style="color: rgb(3, 155, 229); text-decoration: none; margin-bottom: 0px;">CAMERA</a></code></td><td style="padding: 4px 12px; vertical-align: top; border: 1px solid rgb(221, 221, 221); background-color: inherit;"><ul style="margin: 0px 0px 0px 20px; padding: 0px;"><li style="margin: 0px;"><code style="font-size: 13px; color: rgb(0, 102, 0); font-stretch: normal; line-height: 18px; font-family: Consolas, "Liberation Mono", Menlo, Monaco, Courier, monospace; -webkit-font-smoothing: subpixel-antialiased; margin-bottom: 0px;"><a target=_blank href="https://developer.android.com/reference/android/Manifest.permission.html#CAMERA" style="color: rgb(3, 155, 229); text-decoration: none; margin-bottom: 0px;">CAMERA</a></code></li></ul></td></tr><tr><td style="padding: 4px 12px; vertical-align: top; border: 1px solid rgb(221, 221, 221); background-color: inherit;"><code style="font-size: 13px; color: rgb(0, 102, 0); font-stretch: normal; line-height: 18px; font-family: Consolas, "Liberation Mono", Menlo, Monaco, Courier, monospace; -webkit-font-smoothing: subpixel-antialiased; margin-bottom: 0px;"><a target=_blank href="https://developer.android.com/reference/android/Manifest.permission_group.html#CONTACTS" style="color: rgb(3, 155, 229); text-decoration: none; margin-bottom: 0px;">CONTACTS</a></code></td><td style="padding: 4px 12px; vertical-align: top; border: 1px solid rgb(221, 221, 221); background-color: inherit;"><ul style="margin: 0px 0px 0px 20px; padding: 0px;"><li style="margin: 0px 0px 12px;"><code style="font-size: 13px; color: rgb(0, 102, 0); font-stretch: normal; line-height: 18px; font-family: Consolas, "Liberation Mono", Menlo, Monaco, Courier, monospace; -webkit-font-smoothing: subpixel-antialiased; margin-bottom: 0px;"><a target=_blank href="https://developer.android.com/reference/android/Manifest.permission.html#READ_CONTACTS" style="color: rgb(3, 155, 229); text-decoration: none; margin-bottom: 0px;">READ_CONTACTS</a></code></li><li style="margin: 0px 0px 12px;"><code style="font-size: 13px; color: rgb(0, 102, 0); font-stretch: normal; line-height: 18px; font-family: Consolas, "Liberation Mono", Menlo, Monaco, Courier, monospace; -webkit-font-smoothing: subpixel-antialiased; margin-bottom: 0px;"><a target=_blank href="https://developer.android.com/reference/android/Manifest.permission.html#WRITE_CONTACTS" style="color: rgb(3, 155, 229); text-decoration: none; margin-bottom: 0px;">WRITE_CONTACTS</a></code></li><li style="margin: 0px;"><code style="font-size: 13px; color: rgb(0, 102, 0); font-stretch: normal; line-height: 18px; font-family: Consolas, "Liberation Mono", Menlo, Monaco, Courier, monospace; -webkit-font-smoothing: subpixel-antialiased; margin-bottom: 0px;"><a target=_blank href="https://developer.android.com/reference/android/Manifest.permission.html#GET_ACCOUNTS" style="color: rgb(3, 155, 229); text-decoration: none; margin-bottom: 0px;">GET_ACCOUNTS</a></code></li></ul></td></tr><tr><td style="padding: 4px 12px; vertical-align: top; border: 1px solid rgb(221, 221, 221); background-color: inherit;"><code style="font-size: 13px; color: rgb(0, 102, 0); font-stretch: normal; line-height: 18px; font-family: Consolas, "Liberation Mono", Menlo, Monaco, Courier, monospace; -webkit-font-smoothing: subpixel-antialiased; margin-bottom: 0px;"><a target=_blank href="https://developer.android.com/reference/android/Manifest.permission_group.html#LOCATION" style="color: rgb(3, 155, 229); text-decoration: none; margin-bottom: 0px;">LOCATION</a></code></td><td style="padding: 4px 12px; vertical-align: top; border: 1px solid rgb(221, 221, 221); background-color: inherit;"><ul style="margin: 0px 0px 0px 20px; padding: 0px;"><li style="margin: 0px 0px 12px;"><code style="font-size: 13px; color: rgb(0, 102, 0); font-stretch: normal; line-height: 18px; font-family: Consolas, "Liberation Mono", Menlo, Monaco, Courier, monospace; -webkit-font-smoothing: subpixel-antialiased; margin-bottom: 0px;"><a target=_blank href="https://developer.android.com/reference/android/Manifest.permission.html#ACCESS_FINE_LOCATION" style="color: rgb(3, 155, 229); text-decoration: none; margin-bottom: 0px;">ACCESS_FINE_LOCATION</a></code></li><li style="margin: 0px;"><code style="font-size: 13px; color: rgb(0, 102, 0); font-stretch: normal; line-height: 18px; font-family: Consolas, "Liberation Mono", Menlo, Monaco, Courier, monospace; -webkit-font-smoothing: subpixel-antialiased; margin-bottom: 0px;"><a target=_blank href="https://developer.android.com/reference/android/Manifest.permission.html#ACCESS_COARSE_LOCATION" style="color: rgb(3, 155, 229); text-decoration: none; margin-bottom: 0px;">ACCESS_COARSE_LOCATION</a></code></li></ul></td></tr><tr><td style="padding: 4px 12px; vertical-align: top; border: 1px solid rgb(221, 221, 221); background-color: inherit;"><code style="font-size: 13px; color: rgb(0, 102, 0); font-stretch: normal; line-height: 18px; font-family: Consolas, "Liberation Mono", Menlo, Monaco, Courier, monospace; -webkit-font-smoothing: subpixel-antialiased; margin-bottom: 0px;"><a target=_blank href="https://developer.android.com/reference/android/Manifest.permission_group.html#MICROPHONE" style="color: rgb(3, 155, 229); text-decoration: none; margin-bottom: 0px;">MICROPHONE</a></code></td><td style="padding: 4px 12px; vertical-align: top; border: 1px solid rgb(221, 221, 221); background-color: inherit;"><ul style="margin: 0px 0px 0px 20px; padding: 0px;"><li style="margin: 0px;"><code style="font-size: 13px; color: rgb(0, 102, 0); font-stretch: normal; line-height: 18px; font-family: Consolas, "Liberation Mono", Menlo, Monaco, Courier, monospace; -webkit-font-smoothing: subpixel-antialiased; margin-bottom: 0px;"><a target=_blank href="https://developer.android.com/reference/android/Manifest.permission.html#RECORD_AUDIO" style="color: rgb(3, 155, 229); text-decoration: none; margin-bottom: 0px;">RECORD_AUDIO</a></code></li></ul></td></tr><tr><td style="padding: 4px 12px; vertical-align: top; border: 1px solid rgb(221, 221, 221); background-color: inherit;"><code style="font-size: 13px; color: rgb(0, 102, 0); font-stretch: normal; line-height: 18px; font-family: Consolas, "Liberation Mono", Menlo, Monaco, Courier, monospace; -webkit-font-smoothing: subpixel-antialiased; margin-bottom: 0px;"><a target=_blank href="https://developer.android.com/reference/android/Manifest.permission_group.html#PHONE" style="color: rgb(3, 155, 229); text-decoration: none; margin-bottom: 0px;">PHONE</a></code></td><td style="padding: 4px 12px; vertical-align: top; border: 1px solid rgb(221, 221, 221); background-color: inherit;"><ul style="margin: 0px 0px 0px 20px; padding: 0px;"><li style="margin: 0px 0px 12px;"><code style="font-size: 13px; color: rgb(0, 102, 0); font-stretch: normal; line-height: 18px; font-family: Consolas, "Liberation Mono", Menlo, Monaco, Courier, monospace; -webkit-font-smoothing: subpixel-antialiased; margin-bottom: 0px;"><a target=_blank href="https://developer.android.com/reference/android/Manifest.permission.html#READ_PHONE_STATE" style="color: rgb(3, 155, 229); text-decoration: none; margin-bottom: 0px;">READ_PHONE_STATE</a></code></li><li style="margin: 0px 0px 12px;"><code style="font-size: 13px; color: rgb(0, 102, 0); font-stretch: normal; line-height: 18px; font-family: Consolas, "Liberation Mono", Menlo, Monaco, Courier, monospace; -webkit-font-smoothing: subpixel-antialiased; margin-bottom: 0px;"><a target=_blank href="https://developer.android.com/reference/android/Manifest.permission.html#CALL_PHONE" style="color: rgb(3, 155, 229); text-decoration: none; margin-bottom: 0px;">CALL_PHONE</a></code></li><li style="margin: 0px 0px 12px;"><code style="font-size: 13px; color: rgb(0, 102, 0); font-stretch: normal; line-height: 18px; font-family: Consolas, "Liberation Mono", Menlo, Monaco, Courier, monospace; -webkit-font-smoothing: subpixel-antialiased; margin-bottom: 0px;"><a target=_blank href="https://developer.android.com/reference/android/Manifest.permission.html#READ_CALL_LOG" style="color: rgb(3, 155, 229); text-decoration: none; margin-bottom: 0px;">READ_CALL_LOG</a></code></li><li style="margin: 0px 0px 12px;"><code style="font-size: 13px; color: rgb(0, 102, 0); font-stretch: normal; line-height: 18px; font-family: Consolas, "Liberation Mono", Menlo, Monaco, Courier, monospace; -webkit-font-smoothing: subpixel-antialiased; margin-bottom: 0px;"><a target=_blank href="https://developer.android.com/reference/android/Manifest.permission.html#WRITE_CALL_LOG" style="color: rgb(3, 155, 229); text-decoration: none; margin-bottom: 0px;">WRITE_CALL_LOG</a></code></li><li style="margin: 0px 0px 12px;"><code style="font-size: 13px; color: rgb(0, 102, 0); font-stretch: normal; line-height: 18px; font-family: Consolas, "Liberation Mono", Menlo, Monaco, Courier, monospace; -webkit-font-smoothing: subpixel-antialiased; margin-bottom: 0px;"><a target=_blank href="https://developer.android.com/reference/android/Manifest.permission.html#ADD_VOICEMAIL" style="color: rgb(3, 155, 229); text-decoration: none; margin-bottom: 0px;">ADD_VOICEMAIL</a></code></li><li style="margin: 0px 0px 12px;"><code style="font-size: 13px; color: rgb(0, 102, 0); font-stretch: normal; line-height: 18px; font-family: Consolas, "Liberation Mono", Menlo, Monaco, Courier, monospace; -webkit-font-smoothing: subpixel-antialiased; margin-bottom: 0px;"><a target=_blank href="https://developer.android.com/reference/android/Manifest.permission.html#USE_SIP" style="color: rgb(3, 155, 229); text-decoration: none; margin-bottom: 0px;">USE_SIP</a></code></li><li style="margin: 0px;"><code style="font-size: 13px; color: rgb(0, 102, 0); font-stretch: normal; line-height: 18px; font-family: Consolas, "Liberation Mono", Menlo, Monaco, Courier, monospace; -webkit-font-smoothing: subpixel-antialiased; margin-bottom: 0px;"><a target=_blank href="https://developer.android.com/reference/android/Manifest.permission.html#PROCESS_OUTGOING_CALLS" style="color: rgb(3, 155, 229); text-decoration: none; margin-bottom: 0px;">PROCESS_OUTGOING_CALLS</a></code></li></ul></td></tr><tr><td style="padding: 4px 12px; vertical-align: top; border: 1px solid rgb(221, 221, 221); background-color: inherit;"><code style="font-size: 13px; color: rgb(0, 102, 0); font-stretch: normal; line-height: 18px; font-family: Consolas, "Liberation Mono", Menlo, Monaco, Courier, monospace; -webkit-font-smoothing: subpixel-antialiased; margin-bottom: 0px;"><a target=_blank href="https://developer.android.com/reference/android/Manifest.permission_group.html#SENSORS" style="color: rgb(3, 155, 229); text-decoration: none; margin-bottom: 0px;">SENSORS</a></code></td><td style="padding: 4px 12px; vertical-align: top; border: 1px solid rgb(221, 221, 221); background-color: inherit;"><ul style="margin: 0px 0px 0px 20px; padding: 0px;"><li style="margin: 0px;"><code style="font-size: 13px; color: rgb(0, 102, 0); font-stretch: normal; line-height: 18px; font-family: Consolas, "Liberation Mono", Menlo, Monaco, Courier, monospace; -webkit-font-smoothing: subpixel-antialiased; margin-bottom: 0px;"><a target=_blank href="https://developer.android.com/reference/android/Manifest.permission.html#BODY_SENSORS" style="color: rgb(3, 155, 229); text-decoration: none; margin-bottom: 0px;">BODY_SENSORS</a></code></li></ul></td></tr><tr><td style="padding: 4px 12px; vertical-align: top; border: 1px solid rgb(221, 221, 221); background-color: inherit;"><code style="font-size: 13px; color: rgb(0, 102, 0); font-stretch: normal; line-height: 18px; font-family: Consolas, "Liberation Mono", Menlo, Monaco, Courier, monospace; -webkit-font-smoothing: subpixel-antialiased; margin-bottom: 0px;"><a target=_blank href="https://developer.android.com/reference/android/Manifest.permission_group.html#SMS" style="color: rgb(3, 155, 229); text-decoration: none; margin-bottom: 0px;">SMS</a></code></td><td style="padding: 4px 12px; vertical-align: top; border: 1px solid rgb(221, 221, 221); background-color: inherit;"><ul style="margin: 0px 0px 0px 20px; padding: 0px;"><li style="margin: 0px 0px 12px;"><code style="font-size: 13px; color: rgb(0, 102, 0); font-stretch: normal; line-height: 18px; font-family: Consolas, "Liberation Mono", Menlo, Monaco, Courier, monospace; -webkit-font-smoothing: subpixel-antialiased; margin-bottom: 0px;"><a target=_blank href="https://developer.android.com/reference/android/Manifest.permission.html#SEND_SMS" style="color: rgb(3, 155, 229); text-decoration: none; margin-bottom: 0px;">SEND_SMS</a></code></li><li style="margin: 0px 0px 12px;"><code style="font-size: 13px; color: rgb(0, 102, 0); font-stretch: normal; line-height: 18px; font-family: Consolas, "Liberation Mono", Menlo, Monaco, Courier, monospace; -webkit-font-smoothing: subpixel-antialiased; margin-bottom: 0px;"><a target=_blank href="https://developer.android.com/reference/android/Manifest.permission.html#RECEIVE_SMS" style="color: rgb(3, 155, 229); text-decoration: none; margin-bottom: 0px;">RECEIVE_SMS</a></code></li><li style="margin: 0px 0px 12px;"><code style="font-size: 13px; color: rgb(0, 102, 0); font-stretch: normal; line-height: 18px; font-family: Consolas, "Liberation Mono", Menlo, Monaco, Courier, monospace; -webkit-font-smoothing: subpixel-antialiased; margin-bottom: 0px;"><a target=_blank href="https://developer.android.com/reference/android/Manifest.permission.html#READ_SMS" style="color: rgb(3, 155, 229); text-decoration: none; margin-bottom: 0px;">READ_SMS</a></code></li><li style="margin: 0px 0px 12px;"><code style="font-size: 13px; color: rgb(0, 102, 0); font-stretch: normal; line-height: 18px; font-family: Consolas, "Liberation Mono", Menlo, Monaco, Courier, monospace; -webkit-font-smoothing: subpixel-antialiased; margin-bottom: 0px;"><a target=_blank href="https://developer.android.com/reference/android/Manifest.permission.html#RECEIVE_WAP_PUSH" style="color: rgb(3, 155, 229); text-decoration: none; margin-bottom: 0px;">RECEIVE_WAP_PUSH</a></code></li><li style="margin: 0px;"><code style="font-size: 13px; color: rgb(0, 102, 0); font-stretch: normal; line-height: 18px; font-family: Consolas, "Liberation Mono", Menlo, Monaco, Courier, monospace; -webkit-font-smoothing: subpixel-antialiased; margin-bottom: 0px;"><a target=_blank href="https://developer.android.com/reference/android/Manifest.permission.html#RECEIVE_MMS" style="color: rgb(3, 155, 229); text-decoration: none; margin-bottom: 0px;">RECEIVE_MMS</a></code></li></ul></td></tr><tr><td style="padding: 4px 12px; vertical-align: top; border: 1px solid rgb(221, 221, 221); background-color: inherit;"><code style="font-size: 13px; color: rgb(0, 102, 0); font-stretch: normal; line-height: 18px; font-family: Consolas, "Liberation Mono", Menlo, Monaco, Courier, monospace; -webkit-font-smoothing: subpixel-antialiased; margin-bottom: 0px;"><a target=_blank href="https://developer.android.com/reference/android/Manifest.permission_group.html#STORAGE" style="color: rgb(3, 155, 229); text-decoration: none; margin-bottom: 0px;">STORAGE</a></code></td><td style="padding: 4px 12px; vertical-align: top; border: 1px solid rgb(221, 221, 221); background-color: inherit;"><ul style="margin: 0px 0px 0px 20px; padding: 0px;"><li style="margin: 0px 0px 12px;"><code style="font-size: 13px; color: rgb(0, 102, 0); font-stretch: normal; line-height: 18px; font-family: Consolas, "Liberation Mono", Menlo, Monaco, Courier, monospace; -webkit-font-smoothing: subpixel-antialiased; margin-bottom: 0px;"><a target=_blank href="https://developer.android.com/reference/android/Manifest.permission.html#READ_EXTERNAL_STORAGE" style="color: rgb(3, 155, 229); text-decoration: none; margin-bottom: 0px;">READ_EXTERNAL_STORAGE</a></code></li><li style="margin: 0px;"><code style="font-size: 13px; color: rgb(0, 102, 0); font-stretch: normal; line-height: 18px; font-family: Consolas, "Liberation Mono", Menlo, Monaco, Courier, monospace; -webkit-font-smoothing: subpixel-antialiased; margin-bottom: 0px;"><a target=_blank href="https://developer.android.com/reference/android/Manifest.permission.html#WRITE_EXTERNAL_STORAGE" style="color: rgb(3, 155, 229); text-decoration: none; margin-bottom: 0px;">WRITE_EXTERNAL_STORAGE</a></code></li></ul></td></tr></tbody></table><a target=_blank name="declaring" style="color: rgb(3, 155, 229); font-family: Roboto, sans-serif; font-size: 14px; line-height: 24px;"></a><span style="color: rgba(0, 0, 0, 0.682353); font-family: Roboto, sans-serif; font-size: 14px; line-height: 24px;"></span><h2 id="defining" style="color: rgba(0, 0, 0, 0.870588); clear: left; font-size: 28px; font-weight: 400; line-height: 32px; margin: -64px 0px 0px; padding: 12px 0px 0px; border-top-width: 64px; border-top-style: solid; border-top-color: transparent; -webkit-background-clip: padding-box; font-family: Roboto, sans-serif; background-clip: padding-box;">Defining and Enforcing Permissions</h2>