前提:
android6.0以上 并且tragetVersion >22的时候需要主动去做权限管理
基于RxPermission1.0 & RxJava 1.0
RxPermission 1.0: https://github.com/tbruyelle/RxPermissions
rxjava 1.0 :https://github.com/ReactiveX/RxJava/tree/1.x
两个都必须要导入,否则找不到类。
package com.zy.demo;
import android.Manifest;
import android.app.Activity;
import android.util.Log;
import com.tbruyelle.rxpermissions.Permission;
import com.tbruyelle.rxpermissions.RxPermissions;
import rx.functions.Action1;
/**
* @author luchao
* @time 18/6/28 上午10:43
*/
/**
* danger permission danger permission 这些是必须要要去主动获取的
* 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
*/
public class PermissionsUtil {
private static final String TAG = "TT";
/**
* 初始化的时候使用,请求固定的权限列表
* @param activity
*/
public static void requestPermissionList(Activity activity) {
new RxPermissions(activity)
.requestEach(
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_PHONE_STATE
).subscribe(new Action1<Permission>() {
@Override
public void call(Permission permission) {
if(permission.granted){
Log.d(TAG, "has permission1111");
}else{
Log.d(TAG, "no permission2222");
}
}
});
}
/**
* 当调用某个放法,如打开相机的时候,如果之前没有授权,需要再次获取权限,然后产生回调。
* @param activity
*/
public static void requestPermissionSingle(Activity activity, String permission, Action1<Permission>action1) {
new RxPermissions(activity)
.requestEach(permission
).subscribe(action1);
}
/**
* 当调用某个放法,如获取deviceId时候,如果之前没有授权,需要再次获取权限,不需要回调。
* @param activity
*/
public static void requestPermissionSingle(Activity activity, String permission) {
new RxPermissions(activity)
.request(permission
).subscribe(new Action1<Boolean>() {
@Override
public void call(Boolean isPermission) {
}
});
}
}