1.项目的git地址
https://github.com/lantian0314/StudioTest.git
2.首先配置app中的build.gradle以及manifest
compile 'com.tbruyelle.rxpermissions:rxpermissions:0.9.4@aar'
compile 'io.reactivex:rxandroid:1.0.1'
compile 'io.reactivex:rxjava:1.0.14'
manifest权限配置
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
3.下面的代码是申请动态权限功能
private void requestRxPermission(String... permissions) {
RxPermissions rxPermissions = new RxPermissions(this);
rxPermissions.request(permissions).subscribe(new Observer<Boolean>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(Boolean result) {
if (result) {
Toast.makeText(RxPermission.this, "同意权限", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(RxPermission.this, "拒绝权限", Toast.LENGTH_LONG).show();
}
}
});
}
4.可以添加一个权限检查的功能
public static String checkPermission(Context context,String ... permissions){
StringBuilder builder=new StringBuilder();
try {
for (String permission:permissions
) {
builder.append(permission);
builder.append(" is apply state : ");
builder.append(checkPermissionState(context,permission));
builder.append("\n\n");
}
return builder.toString();
}catch (Exception e){
}finally {
if (builder!=null){
builder.delete(0,builder.length());
}
}
return "";
}
private static boolean checkPermissionState(Context context,String permission){
if (context==null|| TextUtils.isEmpty(permission)) {
return false;
}
try {
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M) {
int tragetVersion = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA).targetSdkVersion;
if (tragetVersion >= Build.VERSION_CODES.M) {
return context.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED;
} else {
return PermissionChecker.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
}
}
}catch (Exception e){
}
return false;
}