一、前言
前言?哪来那么多废话,直接进入正题!
二、需要申请的权限
咳咳咳,进入正题前,我们还是先了解一下哪些权限是Android6.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
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
是不是感受到无比的贴心,专门去翻译成了中文,我们可以看到,需要申请的权限都是按组(group)来划分,同组下的权限,只要申请其中一个,那么其他权限就不需要再手动申请了。还是举个栗子吧,例如“数据读写的权限”,我们手动申请了WRITE_EXTERNAL_STORAGE
,那么我们就不用再手动申请READ_EXTERNAL_STORAGE
,好了,开始接下来的表演。
三、添加依赖
在build.gradle中添加如下代码:
dependencies {
···
compile 'com.tbruyelle.rxpermissions2:rxpermissions:0.9.4@aar'
compile "io.reactivex.rxjava2:rxjava:2.1.0"
}
四、使用
在需要申请权限的Activity中调用如下代码初始化:
RxPermissions rxPermissions = new RxPermissions(this);
申请你需要获取的权限,老规矩,举个栗子:
rxPermissions
//request中添加你要获取的权限名称,这里已读写权限为例
.request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
.subscribe(new Consumer<Boolean>() {
@Override
public void accept(@NonNull Boolean aBoolean) throws Exception {
//根据Boolean来判断申请成功后和申请失败后的逻辑判断
if (aBoolean) {
Toast.makeText(MainActivity.this, "申请成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "取消申请", Toast.LENGTH_SHORT).show();
}
}
});
这样就完成了权限申请。当然,如果你要同时申请多个权限,只需要在request中继续添加权限即可:
rxPermissions
//request中申请多个权限
.request(Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CALL_PHONE,
Manifest.permission.CAMERA)
.subscribe(new Consumer<Boolean>() {
@Override
public void accept(@NonNull Boolean aBoolean) throws Exception {
//根据Boolean来判断申请成功后和申请失败后的逻辑判断
if (aBoolean) {
Toast.makeText(MainActivity.this, "申请成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "取消申请", Toast.LENGTH_SHORT).show();
}
}
});
Android6.0权限申请就被我们如此简单的完成啦,是不是炒鸡方便!好了,教程就此结束,喜欢的朋友支持一下,谢谢啦~
网友评论