//定义
private AlertDialog dialog;
private String[] permissions = {Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE};
//调用
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// 检查该权限是否已经获取
int i = ContextCompat.checkSelfPermission(this, permissions[0]);
int f = ContextCompat.checkSelfPermission(this, permissions[1]);
int p = ContextCompat.checkSelfPermission(this, permissions[2]);
// 权限是否已经 授权 GRANTED---授权 DINIED---拒绝
if (i != PackageManager.PERMISSION_GRANTED) {
// 如果没有授予该权限,就去提示用户请求
showDialogTipUserRequestPermission();
} else if (f != PackageManager.PERMISSION_GRANTED) {
showDialogTipUserRequestPermissionf();
}else if(p!=PackageManager.PERMISSION_GRANTED){
showDialogTipUserRequestPermissionp();
}
}
//使用
private void showDialogTipUserRequestPermission() {
new AlertDialog.Builder(this)
.setTitle("系统的服务信息权限不可用")
.setMessage("由于需要获取系统的服务信息,为你显示基站信息;\n否则,您将无法正常使用本程序")
.setPositiveButton("立即开启", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startRequestPermission();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
}).setCancelable(false).show();
}
private void showDialogTipUserRequestPermissionf() {
new AlertDialog.Builder(this)
.setTitle("sd卡储存权限不可用")
.setMessage("由于需要sd卡储存功能;\n否则,您将无法正常使用本程序")
.setPositiveButton("立即开启", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startRequestPermission();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
}).setCancelable(false).show();
}
private void showDialogTipUserRequestPermissionp() {
new AlertDialog.Builder(this)
.setTitle("系统的服务信息权限不可用")
.setMessage("由于需要获取系统的服务信息,为你显示基站信息;\n否则,您将无法正常使用本程序")
.setPositiveButton("立即开启", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startRequestPermission();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
}).setCancelable(false).show();
}
// 开始提交请求权限
private void startRequestPermission() {
ActivityCompat.requestPermissions(this, permissions, 321);
}
// 用户权限 申请 的回调方法
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 321) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
// 判断用户是否 点击了不再提醒。(检测该权限是否还可以申请)
boolean b = shouldShowRequestPermissionRationale(permissions[0]);
if (!b) {
// 用户还是想用我的 APP 的
// 提示用户去应用设置界面手动开启权限
showDialogTipUserGoToAppSettting();
} else
finish();
} else {
Toast.makeText(this, "权限获取成功", Toast.LENGTH_SHORT).show();
}
}
}
}
// 提示用户去应用设置界面手动开启权限
private void showDialogTipUserGoToAppSettting() {
dialog = new AlertDialog.Builder(this)
.setTitle("系统的服务信息权限不可用")
.setMessage("请在-应用设置-权限-中,允许使用系统的服务信息权限")
.setPositiveButton("立即开启", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 跳转到应用设置界面
goToAppSetting();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
}).setCancelable(false).show();
}
// 跳转到当前应用的设置界面
private void goToAppSetting() {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, 123);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 123) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// 检查该权限是否已经获取
int i = ContextCompat.checkSelfPermission(this, permissions[0]);
// 权限是否已经 授权 GRANTED---授权 DINIED---拒绝
if (i != PackageManager.PERMISSION_GRANTED) {
// 提示用户应该去应用设置界面手动开启权限
showDialogTipUserGoToAppSettting();
} else {
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
Toast.makeText(this, "权限获取成功", Toast.LENGTH_SHORT).show();
}
}
}
}