/**
*權(quán)限獲取頁面
*
* Created by wangchenlong on 16/1/26.
*/
public classPermissionsActivityextendsAppCompatActivity {
public static final intPERMISSIONS_GRANTED=0;//權(quán)限授權(quán)
public static final intPERMISSIONS_DENIED=1;//權(quán)限拒絕
private static final intPERMISSION_REQUEST_CODE=0;//系統(tǒng)權(quán)限管理頁面的參數(shù)
private static finalStringEXTRA_PERMISSIONS=
"me.chunyu.clwang.permission.extra_permission";//權(quán)限參數(shù)
private static finalStringPACKAGE_URL_SCHEME="package:";//方案
privatePermissionsCheckermChecker;//權(quán)限檢測器
private booleanisRequireCheck;//是否需要系統(tǒng)權(quán)限檢測
//啟動當(dāng)前權(quán)限頁面的公開接口
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需要使用靜態(tài)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);//請求權(quán)限
}else{
allPermissionsGranted();//全部權(quán)限都已獲取
}
}else{
isRequireCheck=true;
}
}
//返回傳遞的權(quán)限參數(shù)
privateString[] getPermissions() {
returngetIntent().getStringArrayExtra(EXTRA_PERMISSIONS);
}
//請求權(quán)限兼容低版本
private voidrequestPermissions(String... permissions) {
ActivityCompat.requestPermissions(this, permissions,PERMISSION_REQUEST_CODE);
}
//全部權(quán)限均已獲取
private voidallPermissionsGranted() {
setResult(PERMISSIONS_GRANTED);
finish();
}
/**
*用戶權(quán)限處理,
*如果全部獲取,則直接過.
*如果權(quán)限缺失,則提示Dialog.
*
*@paramrequestCode請求碼
*@parampermissions權(quán)限
*@paramgrantResults結(jié)果
*/
@Override
public voidonRequestPermissionsResult(intrequestCode,@NonNullString[] permissions,@NonNullint[] grantResults) {
if(requestCode ==PERMISSION_REQUEST_CODE&& hasAllPermissionsGranted(grantResults)) {
isRequireCheck=true;
allPermissionsGranted();
}else{
isRequireCheck=false;
showMissingPermissionDialog();
}
}
//含有全部的權(quán)限
private booleanhasAllPermissionsGranted(@NonNullint[] grantResults) {
for(intgrantResult : grantResults) {
if(grantResult == PackageManager.PERMISSION_DENIED) {
return false;
}
}
return true;
}
//顯示缺失權(quán)限提示
private voidshowMissingPermissionDialog() {
AlertDialog.Builder builder =newAlertDialog.Builder(PermissionsActivity.this);
builder.setTitle(R.string.help);
builder.setMessage(R.string.string_help_text);
//拒絕,退出應(yīng)用
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();
}
//啟動應(yīng)用的設(shè)置
private voidstartAppSettings() {
Intent intent =newIntent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse(PACKAGE_URL_SCHEME+ getPackageName()));
startActivity(intent);
}
}