在android6.0版本以后,项目中的一些权限不在是用户安装app 时就去申请了,而是要手动的去申请。总之权限问题在项目中是个敏感的问题,如果处理不好就容易出现问题
下面就来实现权限的动态申请(也要在mainfest中去申请的)
方法1
1、要用到依赖的库(放到libs下,别忘记加权限)
//动态权限申请(Google官方)
compile 'pub.devrel:easypermissions:0.2.1'
2,在你要用到申请权限的类要实现上面依赖带的EasyPermissions.PermissionCallbacks接口
3,申请权限的代码(打电话为例)
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) {//为6.0以下的版本,不需要动态获取
//为6.0以下的版本,不需要动态获取,直接执行你需要的操作,记得在清单文件注册
callPhone("12345678910");
} else {
//为6.0及以上版本,需要动态获取权限
//1 检查权限
String[] perms = {Manifest.permission.CALL_PHONE};//可以同时请求多个权限
if (EasyPermissions.hasPermissions(this, perms)) {
//有权限
callPhone("12345678910");
} else {
//2 申请权限
EasyPermissions.requestPermissions(this, "拨打电话需要申请权限",
RC_CALL_PHONE, perms);
}
}
//必须实现这个方法,才能判断权限是否获取成功重写的方法
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// 必须有这一句,否则接口回调方法无效
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
/**
* 权限请求成功的回调
* @param requestCode //请求码
* @param perms //你所请求的权限名称集合
*/
@Override
public void onPermissionsGranted(int requestCode, List<String> perms) {
switch(requestCode) {
case RC_CALL_PHONE:
callPhone("12345678910");
break;
default:
break;
}
}
/**
* 权限请求失败的回调
* @param requestCode //请求码
* @param perms //你所请求的权限名称集合
*/
@Override
public void onPermissionsDenied(int requestCode, List<String> perms) {
MyToast.show(getApplication(),"请在设置打开拨打电话权限");
}
/**
* 拨打电话
*/
private void callPhone(final String phoneNum) {
final MyAlertDialog dialog = new MyAlertDialog(this)
.builder("0.85").setMsg("确定要拨打电话" + phoneNum + "吗?")
.setNegativeButton("下次再说", new View.OnClickListener() {
@Override
public void onClick(View v) {
// do nothing
}
});
dialog.setPositiveButton("立即拨打", new View.OnClickListener() {
@Override
public void onClick(View v) {
// 点击“确认”后的操作
// 用intent启动拨打电话
try {//这里必须用try,catch,以防止没有获取到权限程序奔溃
Intent iiPhone = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNum));
if (ActivityCompat.checkSelfPermission(Permissions1Activity.this, Manifest.permission.CALL_PHONE)
!= PackageManager.PERMISSION_GRANTED) {
return;
}
startActivity(iiPhone);
} catch (Exception e) {
Log.e("PermissionsActivity","没有获取到应用权限...");
}
}
});
dialog.show();
dialog.setCancelable(true);
}
,方法2,直接代码(照相机为例)
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) {//为6.0以下的版本,不需要动态获取
//为6.0以下的版本,不需要动态获取,直接执行你需要的操作,记得在清单文件注册
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
Environment.getExternalStorageDirectory().getPath(), "head.jpg")));
startActivityForResult(intent, FROM_CAMERA);
} else {
if (ContextCompat.checkSelfPermission(MyDetailActivity.this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
//申请WRITE_EXTERNAL_STORAGE权限
ActivityCompat.requestPermissions(MyDetailActivity.this, new String[]{Manifest.permission.CAMERA},
1);
} else {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
Environment.getExternalStorageDirectory().getPath(), "head.jpg")));
startActivityForResult(intent, FROM_CAMERA);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission Granted
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
Environment.getExternalStorageDirectory().getPath(), "head.jpg")));
startActivityForResult(intent, FROM_CAMERA);
} else {
// Permission Denied
// displayFrameworkBugMessageAndExit();
Toast.makeText(this, "请在应用管理中打开“相机”访问权限!", Toast.LENGTH_LONG).show();
finish();
}
}
}