参考文章:
http://www.jianshu.com/p/aa1d4b4360ed
http://www.jianshu.com/p/5dbba72c1769
http://blog.csdn.net/linglongxin24/article/details/53189359
图一:
图二:
图三:
首先判断sdk版本是否大于等于23,如果是进行权限检查,如果缺少权限调到PermissionsActivity中:
if (Build.VERSION.SDK_INT >= 23) {
mPermissionsChecker = new PermissionsChecker(this);
if (mPermissionsChecker.lacksPermissions(PERMISSIONS)) {
PermissionsActivity.startActivityForResult(this, REQUEST_CODE, PERMISSIONS);
return;
}
}
定义了三个权限:
// 所需的全部权限
static final String[] PERMISSIONS = new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.ACCESS_FINE_LOCATION
};
PermissionsChecker: 检查所需的权限是否全部允许:
public class PermissionsChecker {
private final Context mContext;
public PermissionsChecker(Context context) {
mContext = context.getApplicationContext();
}
// 判断权限集合
public boolean lacksPermissions(String... permissions) {
for (String permission : permissions) {
if (lacksPermission(permission)) {
return true;
}
}
return false;
}
// 判断是否缺少权限
private boolean lacksPermission(String permission) {
return ContextCompat.checkSelfPermission(mContext, permission) ==
PackageManager.PERMISSION_DENIED;
}
}
如果缺少权限,就跳转到PermissionsActivity进行设置:
onResume中再次检查,如果缺少权限去请求权限
@Override
protected void onResume() {
super.onResume();
if (isRequireCheck) {
String[] permissions = getPermissions();
if (mChecker.lacksPermissions(permissions)) {
requestPermissions(permissions); // 请求权限
} else {
allPermissionsGranted(); // 全部权限都已获取
}
} else {
isRequireCheck = true;
}
}
请求权限,如图一显示:
private void requestPermissions(String... permissions) {
ActivityCompat.requestPermissions(this, permissions, PERMISSION_REQUEST_CODE);
}
如果权限都有了,关闭PermissionsActivity
// 全部权限均已获取
private void allPermissionsGranted() {
setResult(PERMISSIONS_GRANTED);
finish();
}
一次权限修改后,我们要对此次的权限修改结果进行处理,覆写onRequestPermissionsResult方法就可:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == PERMISSION_REQUEST_CODE && hasAllPermissionsGranted(grantResults)) {
isRequireCheck = true;
allPermissionsGranted();
} else {
isRequireCheck = false;
showMissingPermissionDialog();
}
}
如果还是权限缺失,弹出提示框,让用户退出应用或者去设置里面修改,如图2和图3所示:
// 显示缺失权限提示
private void showMissingPermissionDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(PermissionsActivity.this);
builder.setTitle(R.string.help);
builder.setMessage(R.string.string_help_text);
builder.setCancelable(false);
// 拒绝, 退出应用
builder.setNegativeButton(R.string.quit, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setResult(PERMISSIONS_DENIED);
finish();
}
});
builder.setPositiveButton(R.string.settings, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startAppSettings();
dialog.dismiss();
}
});
builder.show();
}
打开设置页面:
private void startAppSettings() {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse(PACKAGE_URL_SCHEME + getPackageName()));
startActivity(intent);
}