我在android6.0运行权限上的处理是,在一个类中写权限的申请,在需要权限的类中去继承,达到权限申请效果。
1.需要在中AndroidManifest.xml选择需要的权限
/**
* 继承了Activity, 实现Android6.0的运行时权限检测
* 需要进行运行时权限检测的Activity可以继承这个类
*
*@authorzhouyan
*@创建时间:2016年5月27日 下午3:01:3 *
*@项目名称:Project
*@文件名称:PermissionsActivity.java
*@类型名称:PermissionsActivity
*@since1.0.0
*/
public class PermissionsActivity extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback {
/**
* 需要进行检测的权限数组
需要请求的运行时权限写在数组中
*/
protected String[] needPermissions= {
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.CAMERA,
Manifest.permission.CALL_PHONE,
Manifest.permission.WRITE_CONTACTS
};
private static final intPERMISSON_REQUESTCODE=0;
/**
* 判断是否需要检测, 防止不停的弹框
*/
private booleanisNeedCheck=true;
@Override
protected voidonResume() {
super.onResume();
int currentapiVersion=android.os.Build.VERSION.SDK_INT;
System.out.print("currentapiVersion&&&&&&&&&&&&&&&&&&"+currentapiVersion);
if(currentapiVersion<23){
isNeedCheck=false;
}else{
if(isNeedCheck) {
checkPermissions(needPermissions);
}
}
}
/**
*@since2.5.0
*/
private void checkPermissions(String... permissions) {
List
if(null!= needRequestPermissonList
&& needRequestPermissonList.size() >0) {
ActivityCompat.requestPermissions(this,
needRequestPermissonList.toArray(
newString[needRequestPermissonList.size()]),
PERMISSON_REQUESTCODE);
}
}
/**
* 获取权限集中需要申请权限的列表
*
*@parampermissions
*@return
*@since2.5.0
*/
private List
List
for(String perm : permissions) {
if(ContextCompat.checkSelfPermission(this,
perm) != PackageManager.PERMISSION_GRANTED
|| ActivityCompat.shouldShowRequestPermissionRationale(
this, perm)) {
needRequestPermissonList.add(perm);
}
}
returnneedRequestPermissonList;
}
/**
* 检测是否说有的权限都已经授权
*
*@paramgrantResults
*@return
*@since2.5.0
*/
private boolean verifyPermissions(int[] grantResults) {
for(intresult : grantResults) {
if(result != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
@SuppressLint("Override")
@Override
public voidonRequestPermissionsResult(intrequestCode,
String[] permissions,int[] paramArrayOfInt) {
if(requestCode ==PERMISSON_REQUESTCODE) {
if(!verifyPermissions(paramArrayOfInt)) {
showMissingPermissionDialog();
isNeedCheck=false;
}
}
}
/**
* 显示提示信息*@since2.5.0
*/
private void showMissingPermissionDialog() {
AlertDialog.Builder builder =newAlertDialog.Builder(this);
builder.setTitle(R.string.notifyTitle);
builder.setMessage(R.string.notifyMsg);
// 拒绝, 退出应用
builder.setNegativeButton(R.string.cancel,
newDialogInterface.OnClickListener() {
@Override
public voidonClick(DialogInterface dialog,intwhich) {
finish();
}
});
builder.setPositiveButton(R.string.setting,
newDialogInterface.OnClickListener() {
@Override
public voidonClick(DialogInterface dialog,intwhich) {
startAppSettings();
}
});
builder.setCancelable(false);
builder.show();
}
/**
* 启动应用的设置
*
*@since2.5.0
*/
private void startAppSettings() {
Intent intent =newIntent(
Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:"+ getPackageName()));
startActivity(intent);
}
@Override
public boolean onKeyDown(intkeyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK) {
this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
}