1. 运行时获得权限:
从androi 6.0开始,不再是安装应用时用户确定获得全部的权限.而是在使用软件过程中需要该权限时,弹出对话框让用户选择权限.不仅如此,用户选择权限后还可以关闭。
2. 检查是否获得权限:
通过 ContextCompat.checkSelfPermission(context,permission)方法,方法返回值为 PackageManager.PERMISSION_GRANTED or PackageManager.PERMISSION_DENIED
int permissionCheck = ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_CALENDAR);
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(PermissionActivity.this, "hava this permission", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(PermissionActivity.this, "no this permission", Toast.LENGTH_SHORT).show();
3. 请求权限:
通过ActivityCompat.requestPermissions(activity,permissions,requestCode)第二个参数是一个String数组,第三个参数是请求码便于在onRequestPermissionsResult 方法中根据requestCode进行判断.
ActivityCompat.requestPermissions(PermissionActivity.this,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
4. 请求权限后的回调:
请求权限后会回调onRequestPermissionsResult方法,在activity中重写onRequestPermissionsResult(requestCode,permissions,grantResults)方法 grantResults是int类型的数组每个值为PackageManager.PERMISSION_GRANTED or PackageManager.PERMISSION_DENIED 分别对应permissions的每个请求.
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull 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) {
} else {
5. 告诉用户为何需要权限:
ActivityCompat.shouldShowRequestPermissionRationale(activity,permission) 这个方法是在用户拒绝权限后返回true. 也就是说:用户第一次点击一个需要权限的地方,该方法返回false(因为用户没拒绝~),当用户拒绝掉该权限,下次点击此权限处,该方法会返回true.可在里面进行对该权限的说明,然后弹出权限让用户选择,并且对话框有don't ask again选项.
@OnClick(R.id.btn_check)
public void onClick() {
//判断是否有权限
if (ContextCompat.checkSelfPermission(PermissionActivity.this,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
//当用户拒绝掉权限时.
if (ActivityCompat.shouldShowRequestPermissionRationale(PermissionActivity.this,
Manifest.permission.READ_CONTACTS)) {
Toast.makeText(PermissionActivity.this, "true", Toast.LENGTH_SHORT).show();
AlertDialog dialog = new AlertDialog.Builder(this).setTitle("该权限保证手机不会爆炸^ ^").setPositiveButton("我需要此权限!", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(PermissionActivity.this,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
}).setNegativeButton("炸吧炸吧~", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(PermissionActivity.this, "准备爆炸了", Toast.LENGTH_SHORT).show();
}).show();
} else {
Toast.makeText(PermissionActivity.this, "false", Toast.LENGTH_SHORT).show();
ActivityCompat.requestPermissions(PermissionActivity.this,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
6.用户选择do't ask again 后
ActivityCompat.shouldShowRequestPermissionRationale(activity,permission) 方法一直返回false,并且ActivityCompat.requestPermissions不会弹出对话框,系统直接deny,并回调onRequestPermissionsResult方法.
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull 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) {
} else {
showDialog();
// Toast.makeText(PermissionActivity.this, "request false", Toast.LENGTH_SHORT).show();
private void showDialog() {
AlertDialog dialog = new AlertDialog.Builder(this).setTitle("还可以手动开启哦~").setMessage("可以前往设置->app->myapp->permission打开").setPositiveButton("确定!", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}).show();
android权限有normal和dangerous两种 normal系统会自动给予权限,dangerous需要用户选择.Normal and Dangerous Permissions
用户手动开启权限: