Android6.0运行时权限以及RxPermissions的使用

时间:2022-07-15 09:31:37

普通方式请求

以拨打电话为例

@OnClick(R.id.btnCommon)
public void CommonCall() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) !=
PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 0);
}else {
startActivity(new Intent(Intent.ACTION_CALL).setData(Uri.parse("tel:" + 10000)));
}
}
 @Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions,@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 0) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startActivity(new Intent(Intent.ACTION_CALL).setData(Uri.parse("tel:" + 10000)));
}
}
}

RxPermissions方式请求

RxPermissions github 传送门RxPermissions

     //RxJava的依赖包
compile 'io.reactivex.rxjava2:rxjava:'+rxjava_version
//RxAndroid的依赖包
compile 'io.reactivex.rxjava2:rxandroid:'+rxjava_version
//RxPermissions
compile 'com.tbruyelle.rxpermissions2:rxpermissions:0.9.4@aar'
  • 首先需要做初始化工作
RxPermissions rxPermissions = new RxPermissions(this);
@OnClick(R.id.btnRxPermission)
public void RxPermissionCall() {
rxPermissions.request(Manifest.permission.CALL_PHONE).subscribe(new Consumer<Boolean>() {
@Override
public void accept(Boolean granted) throws Exception {
if (granted) {
startActivity(new Intent(Intent.ACTION_CALL).setData(Uri.parse("tel:" + 10000)));
}
}
});
}

如果使用了java8 这段代码将会是这样 非常简洁

@OnClick(R.id.btnRxPermission)
public void RxPermissionCall() {
rxPermissions.request(Manifest.permission.CALL_PHONE).subscribe(granted -> {
if (granted) {
startActivity(new Intent(Intent.ACTION_CALL).setData(Uri.parse("tel:" + 10000)));
}
});
}

RxPermissions 请求多个权限

rxPermissions
.requestEach(Manifest.permission.CAMERA,
Manifest.permission.READ_PHONE_STATE)
.subscribe(permission -> { // will emit 2 Permission objects
if (permission.granted) {
// `permission.name` is granted !
} else if (permission.shouldShowRequestPermissionRationale)
// Denied permission without ask never again
} else {
// Denied permission with ask never again
// Need to go to the settings
}
});

普通方法Fragment中运行时权限的特殊处理

  • 在Fragment中申请权限,不要使用ActivityCompat.requestPermissions, 直接使用Fragment的requestPermissions方法,否则会回调到Activity的 onRequestPermissionsResult

  • 如果在Fragment中嵌套Fragment,在子Fragment中使用requestPermissions方法,onRequestPermissionsResult不会回调回来,建议使用 getParentFragment().requestPermissions方法,
    这个方法会回调到父Fragment中的onRequestPermissionsResult,加入以下代码可以把回调透传到子Fragment

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
List<Fragment> fragments = getChildFragmentManager().getFragments();
if (fragments != null) {
for (Fragment fragment : fragments) {
if (fragment != null) {
fragment.onRequestPermissionsResult(requestCode,permissions,grantResults);
}
}
}
}