RxPermissions使用詳解

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,983評(píng)論 25 709
  • afinalAfinal是一個(gè)android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,861評(píng)論 2 45
  • 剛上大學(xué)那會(huì)兒,希望能和相伴四年的室友成為至交好友,憧憬著和她們一起笑,一起哭,一起成長(zhǎng)。后來發(fā)現(xiàn)那只不過...
    慕白hf閱讀 310評(píng)論 0 0
  • 16.7ing~世界上很多事情雖然已經(jīng)過去了,但是它卻永久的留在了記憶深處。直到現(xiàn)在我依然清晰的記得那時(shí)候每晚拖著...
    平安到永遠(yuǎn)閱讀 241評(píng)論 0 0
  • 我聽著風(fēng)吹動(dòng)著搖鈴,雨滴答滴地落在雨棚上,房間里好靜,靜得連我感覺自己也不在房間里,我像一具木乃伊,不說話也不動(dòng),...
    蕪魚閱讀 307評(píng)論 1 0

友情鏈接更多精彩內(nèi)容