1、介紹:
This library allows the usage of RxJava with the new Android M permission model.
即: 這個(gè)庫(kù)支持RxJava與新的Android M版本權(quán)限模型一起使用。
2、如何依賴
2.1 簡(jiǎn)單使用(不支持RxJava,訂閱)
``` compile 'com.tbruyelle.rxpermissions:rxpermissions:0.9.4@aar' ```
2.2支持RxJava的使用(將rxpermissions包名換為rxpermissions2) 即:
compile 'com.tbruyelle.rxpermissions2:rxpermissions:0.9.4@aar'
3、遷移到0.9 后的說明
官網(wǎng)介紹:
Version 0.9 now uses a retained fragment to trigger the permission request from the framework. As a result, the RxPermissions class is no more a singleton. To migrate from 0.8 or earlier, just replace the following :
即:
版本0.9現(xiàn)在使用支持Fragment去觸發(fā)框架的權(quán)限請(qǐng)求。 因此,RxPermissions類不再功能單一。 要從0.8或更低版本遷移,只需更換以下內(nèi)容:
RxPermissions.getInstance(this) -> new RxPermissions(this) // where this is an Activity instance
4、如何在項(xiàng)目中使用
在初始化Activity中加入如下代碼:
RxPermissions rxPermissions=new RxPermissions(this);
rxPermissions.request(Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.CALL_PHONE,Manifest.permission.INTERNET).subscribe(new Consumer<Boolean>() {
@Override
public void accept(Boolean aBoolean) throws Exception {
if (aBoolean){
//申請(qǐng)的權(quán)限全部允許
Toast.makeText(MainActivity.this, "允許了權(quán)限!", Toast.LENGTH_SHORT).show();
initData();
}else{
//只要有一個(gè)權(quán)限被拒絕,就會(huì)執(zhí)行
Toast.makeText(MainActivity.this, "未授權(quán)權(quán)限,部分功能不能使用", Toast.LENGTH_SHORT).show();
}
}
});
If you need to trigger the permission request from a specific event, you need to setup your event as an observable inside an initialization phase.
即:
如果您需要觸發(fā)特定事件的權(quán)限請(qǐng)求,則需要在初始化階段將事件設(shè)置為可觀察的事件。
You can use JakeWharton/RxBinding to turn your view to an observable (not included in the library).
譯:
您可以使用JakeWharton / RxBinding將您的視圖轉(zhuǎn)換為可觀察對(duì)象(不包含在庫(kù)中)。
例如:
// Must be done during an initialization phase like onCreate
RxView.clicks(findViewById(R.id.enableCamera))
.compose(rxPermissions.ensure(Manifest.permission.CAMERA))
.subscribe(granted -> {
// R.id.enableCamera has been clicked
});
同時(shí)申請(qǐng)多個(gè)權(quán)限:
rxPermissions
.request(Manifest.permission.CAMERA,
Manifest.permission.READ_PHONE_STATE)
.subscribe(granted -> {
if (granted) {
// All requested permissions are granted
} else {
// At least one permission is denied
}
});
監(jiān)聽具體的某一些權(quán)限是否進(jìn)行了授權(quán)
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
}
});
5、狀態(tài)
This library is still beta, so contributions are welcome. I'm currently using it in production since months without issue.
譯:
這個(gè)庫(kù)仍然是測(cè)試版,所以貢獻(xiàn)是值得歡迎的。 目前我正在使用它在生產(chǎn)中沒有問題。
6、優(yōu)點(diǎn):
避免擔(dān)心框架版本。 如果sdk是前M,觀察者將自動(dòng)收到一個(gè)授予的結(jié)果。
防止您在權(quán)限請(qǐng)求和結(jié)果處理之間分割您的代碼。 假如沒有這個(gè)庫(kù),你必須在一個(gè)地方請(qǐng)求權(quán)限,并在Activity.onRequestPermissionsResult()中處理結(jié)果。
所有RX提供的有關(guān)轉(zhuǎn)換,過濾,鏈接...
7、如果要使用訂閱、觀察,必須依賴rxjava庫(kù)如:
// RxJava
compile 'io.reactivex.rxjava2:rxjava:2.0.1'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'com.jakewharton.rxbinding2:rxbinding:2.0.0'
//rxpermissions
compile 'com.tbruyelle.rxpermissions2:rxpermissions:0.9.4@aar'
最后:附上RxPermissions的庫(kù)github地址: https://github.com/tbruyelle/RxPermissions