在6.0下新的权限机制分为两类,普通权限,这类权限是不需要用户授权,不会触及用户的隐私,只需要在manefest里面申请即可;还有一类是危险许可,这类权限需要动态的去申请
官方文档:
https://github.com/googlesamples/easypermissions
运行时权限官方文档解释:
https://developer.android.com/training/permissions/requesting.html
2为什么使用Easypermissions
Android M对于权限系统进行了改变.Android M之前所需权限只需要在AndroidManifest中申请即可。但更新Android M系统后,部分“危险许可”需要在运行时询问申请。
正常和危险权限解释:
https://developer.android.com/guide/topics/security/permissions.html#normal-dangerous
如下图所示:
举个栗子:
下面的栗子是常用的获取用户的写外部存储器和打电话的权限
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)
!= PackageManager.PERMISSION_GRANTED) {//未获取权限
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) {
//当用户第一次申请拒绝时,再次申请该权限调用
Toast.makeText(this, "拨打电话权限", Toast.LENGTH_SHORT).show();
}
//申请权限
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 0x01);
} else {//已经获得了权限
call();
}
}
private void call() {
Toast.makeText(this, "打电话", Toast.LENGTH_SHORT).show();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 0x01) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {//授权成功
call();
} else {//授权失败
Toast.makeText(this, "获取权限失败", Toast.LENGTH_SHORT).show();
}
}
}
运行时权限申请
代码中如果需要申请运行时权限步骤:
1检查运行时权限
//所要申请的权限
String[] perms = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CALL_PHONE};
if (EasyPermissions.hasPermissions(this, perms)) {//检查是否获取该权限
Log.i(TAG, "已获取权限");
} else {
.....
}
2申请权限
//第二个参数是被拒绝后再次申请该权限的解释
//第三个参数是请求码
//第四个参数是要申请的权限
EasyPermissions.requestPermissions(this, "必要的权限", 0, perms);
3接收权限处理结果
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//把申请权限的回调交由EasyPermissions处理
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
//下面两个方法是实现EasyPermissions的EasyPermissions.PermissionCallbacks接口
//分别返回授权成功和失败的权限
@Override
public void onPermissionsGranted(int requestCode, List<String> perms) {
Log.i(TAG, "获取成功的权限" + perms);
}
@Override
public void onPermissionsDenied(int requestCode, List<String> perms) {
Log.i(TAG, "获取失败的权限" + perms);
}
以上即是google建议的运行时权限处理过程。代码量和处理地方还是比较繁琐的.so,Easypermissions应运而生。
3如何使用
首先,引用库
dependencies {
compile 'pub.devrel:easypermissions:0.1.9'
}
同样,easypermissions处理权限分为3步。
1检查权限
String[] perms = {Manifest.permission.CAMERA, Manifest.permission.CHANGE_WIFI_STATE};
if (EasyPermissions.hasPermissions(this, perms)) {
//...
} else {
//...
}
2申请权限
EasyPermissions.requestPermissions(this, "拍照需要摄像头权限",
RC_CAMERA_AND_WIFI, perms);
3实现EasyPermissions.PermissionCallbacks接口,直接处理权限是否成功申请
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// Forward results to EasyPermissions
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
//成功
@Override
public void onPermissionsGranted(int requestCode, List<String> list) {
// Some permissions have been granted
// ...
}
//失败
@Override
public void onPermissionsDenied(int requestCode, List<String> list) {
// Some permissions have been denied
// ...
}
简易主要简化了对权限申请结果的处理和判断,直接以接口的方式回调处理结果。不需要再自行进行处理。