今天讲讲工作中遇见的6.0运行时权限处理问题,起因就是设置版本更新时,在6.0系统会报错,起因就是6.0动态权限设置,因为在google为了安全考虑,对于一些特定权限会征询客户授权,这当然会大大增加客户资料的安全性,当然也会让我们在工作中做一些小小的变动,下面就讲一下我在工作中是如何解决这些问题的
首先copy一下6.0会征求用户授权的权限
//联系人的读写权限
group:android.permission-group.CONTACTS
permission:android.permission.WRITE_CONTACTS
permission:android.permission.GET_ACCOUNTS
permission:android.permission.READ_CONTACTS
//拨打电话权限
group:android.permission-group.PHONE
permission:android.permission.READ_CALL_LOG
permission:android.permission.READ_PHONE_STATE
permission:android.permission.CALL_PHONE
permission:android.permission.WRITE_CALL_LOG
permission:android.permission.USE_SIP
permission:android.permission.PROCESS_OUTGOING_CALLS
permission:com.android.voicemail.permission.ADD_VOICEMAIL
//日历权限
group:android.permission-group.CALENDAR
permission:android.permission.READ_CALENDAR
permission:android.permission.WRITE_CALENDAR
//相机权限
group:android.permission-group.CAMERA
permission:android.permission.CAMERA
//传感器权限
group:android.permission-group.SENSORS
permission:android.permission.BODY_SENSORS
//定位权限
group:android.permission-group.LOCATION
permission:android.permission.ACCESS_FINE_LOCATION
permission:android.permission.ACCESS_COARSE_LOCATION
//内存读写权限
group:android.permission-group.STORAGE
permission:android.permission.READ_EXTERNAL_STORAGE
permission:android.permission.WRITE_EXTERNAL_STORAGE
//录音权限
group:android.permission-group.MICROPHONE
permission:android.permission.RECORD_AUDIO
//SMS短信权限
group:android.permission-group.SMS
permission:android.permission.READ_SMS
permission:android.permission.RECEIVE_WAP_PUSH
permission:android.permission.RECEIVE_MMS
permission:android.permission.RECEIVE_SMS
permission:android.permission.SEND_SMS
permission:android.permission.READ_CELL_BROADCASTS
以上copy自鸿洋大神的介绍,我就不多说了,大家了解一下就行了,下面我以版本更新中的6.0权限为例,来学习一下如何动态添加权限
permission.WRITE_EXTERNAL_STORAGE是6.0权限,版本更新会用到这个权限
private void uploadVersion() {
//对权限进行检测
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
//没有权限时进行申请授权,context,权限数组,int请求码
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1001);
} else {
//有权限时执行版本更新接口
GetVersionEngine engine = new GetVersionEngine(getRequestTag());
engine.sendRequest();
}
}
权限请求的处理
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1001) {//匹配请求码
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//权限设置成功,执行版本更新接口
GetVersionEngine engine = new GetVersionEngine(getRequestTag());
engine.sendRequest();
} else
{
// 权限拒绝,可以进行提示
}
}
}
好啦,动态设置6.0权限还是很简单的,大家了解一下就行了,主要的问题就是你要清楚哪些权限是6.0要动态获取的权限。
下面推荐一个6.0权限使用库,毕竟那么多权限,我们一个一个设置还是很麻烦的,所以要是提前就将我们要用到的6.0动态获取权限都进行授权的话,那就方便多了
PermissionGen
使用方法:
添加依赖:
compile 'com.lovedise:permissiongen:0.0.6'
请求权限的两种方式:
PermissionGen.with(MainActivity.this)
.addRequestCode(100)
.permissions(
Manifest.permission.READ_CONTACTS,
Manifest.permission.RECEIVE_SMS,
Manifest.permission.WRITE_CONTACTS)
.request();
PermissionGen.needPermission(ContactFragment.this, 100,
new String[] {
Manifest.permission.READ_CONTACTS,
Manifest.permission.RECEIVE_SMS,
Manifest.permission.WRITE_CONTACTS
}
);
权限请求结果
@Override public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
PermissionGen.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
}
请求成功
@PermissionSuccess(requestCode = 100)
public void doSomething(){
Toast.makeText(this, "Contact permission is granted", Toast.LENGTH_SHORT).show();
}
请求失败
@PermissionFail(requestCode = 100)
public void doFailSomething(){
Toast.makeText(this, "Contact permission is not granted", t.LENGTH_SHORT).show();
}
这是一个非常简单的6.0权限处理库。大家可以借鉴一下。