/**
*权限获取页面
*
* Created by wangchenlong on 16/1/26.
*/
public classPermissionsActivityextendsAppCompatActivity {
public static final intPERMISSIONS_GRANTED=0;//权限授权
public static final intPERMISSIONS_DENIED=1;//权限拒绝
private static final intPERMISSION_REQUEST_CODE=0;//系统权限管理页面的参数
private static finalStringEXTRA_PERMISSIONS=
"me.chunyu.clwang.permission.extra_permission";//权限参数
private static finalStringPACKAGE_URL_SCHEME="package:";//方案
privatePermissionsCheckermChecker;//权限检测器
private booleanisRequireCheck;//是否需要系统权限检测
//启动当前权限页面的公开接口
public static voidstartActivityForResult(Activity activity,intrequestCode, String... permissions) {
Intent intent =newIntent(activity, PermissionsActivity.class);
intent.putExtra(EXTRA_PERMISSIONS, permissions);
ActivityCompat.startActivityForResult(activity, intent, requestCode,null);
}
@Overrideprotected voidonCreate(@NullableBundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(getIntent() ==null|| !getIntent().hasExtra(EXTRA_PERMISSIONS)) {
throw newRuntimeException("PermissionsActivity需要使用静态startActivityForResult方法启动!");
}
setContentView(R.layout.activity_permissions);
mChecker=newPermissionsChecker(this);
isRequireCheck=true;
}
@Overrideprotected voidonResume() {
super.onResume();
if(isRequireCheck) {
String[] permissions = getPermissions();
if(mChecker.lacksPermissions(permissions)) {
requestPermissions(permissions);//请求权限
}else{
allPermissionsGranted();//全部权限都已获取
}
}else{
isRequireCheck=true;
}
}
//返回传递的权限参数
privateString[] getPermissions() {
returngetIntent().getStringArrayExtra(EXTRA_PERMISSIONS);
}
//请求权限兼容低版本
private voidrequestPermissions(String... permissions) {
ActivityCompat.requestPermissions(this, permissions,PERMISSION_REQUEST_CODE);
}
//全部权限均已获取
private voidallPermissionsGranted() {
setResult(PERMISSIONS_GRANTED);
finish();
}
/**
*用户权限处理,
*如果全部获取,则直接过.
*如果权限缺失,则提示Dialog.
*
*@paramrequestCode请求码
*@parampermissions权限
*@paramgrantResults结果
*/
@Override
public voidonRequestPermissionsResult(intrequestCode,@NonNullString[] permissions,@NonNullint[] grantResults) {
if(requestCode ==PERMISSION_REQUEST_CODE&& hasAllPermissionsGranted(grantResults)) {
isRequireCheck=true;
allPermissionsGranted();
}else{
isRequireCheck=false;
showMissingPermissionDialog();
}
}
//含有全部的权限
private booleanhasAllPermissionsGranted(@NonNullint[] grantResults) {
for(intgrantResult : grantResults) {
if(grantResult == PackageManager.PERMISSION_DENIED) {
return false;
}
}
return true;
}
//显示缺失权限提示
private voidshowMissingPermissionDialog() {
AlertDialog.Builder builder =newAlertDialog.Builder(PermissionsActivity.this);
builder.setTitle(R.string.help);
builder.setMessage(R.string.string_help_text);
//拒绝,退出应用
builder.setNegativeButton(R.string.quit,newDialogInterface.OnClickListener() {
@Overridepublic voidonClick(DialogInterface dialog,intwhich) {
setResult(PERMISSIONS_DENIED);
finish();
}
});
builder.setPositiveButton(R.string.settings,newDialogInterface.OnClickListener() {
@Override
public voidonClick(DialogInterface dialog,intwhich) {
startAppSettings();
}
});
builder.setCancelable(false);
builder.show();
}
//启动应用的设置
private voidstartAppSettings() {
Intent intent =newIntent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse(PACKAGE_URL_SCHEME+ getPackageName()));
startActivity(intent);
}
}