github地址:https://github.com/tbruyelle/RxPermissions
gradle添加依赖如下:
compile ‘com.tbruyelle.rxpermissions2:rxpermissions:0.9.4@aar’
compile “io.reactivex.rxjava2:rxjava:2.0.0”
申请权限:
private void requestPermissions()
{
RxPermissions rxPermission = new RxPermissions(this);
rxPermission
.requestEach(
Manifest.permission.READ_PHONE_STATE)
.subscribe(new Consumer<Permission>()
{
@Override
public void accept(Permission permission) throws Exception
{
if (permission.granted)
{
// 用户已经同意该权限
Toast.makeText(MainActivity.this,"获得权限",Toast.LENGTH_SHORT).show();
} else if (permission.shouldShowRequestPermissionRationale)
{
// 用户拒绝了该权限,没有选中『不再询问』(Never ask again),那么下次再次启动时,还会提示请求权限的对话框
Toast.makeText(MainActivity.this,"拒绝了该权限",Toast.LENGTH_SHORT).show();
} else
{
// 用户拒绝了该权限,并且选中『不再询问』
Toast.makeText(MainActivity.this,"拒绝了该权限,不再询问",Toast.LENGTH_SHORT).show();
}
}
});
}
注意:需要在manifest中申请READ_PHONE_STATE权限,否则上述代码无效