The fifth unit rxpermissions, the use of rxlifecycle

Introduction of rxpermissions

https://github.com/tbruyelle/RxPermissions?spm=a2c4e.10696291.0.0.44a719a4ahaTWq

Note that the version number of Android must be the version 3.0 of rxjava. After that, the machine must be 26 or more. And the emulator 8.0 is wrong. So go directly to 9.0

Guide package

allprojects {
    
    
    repositories {
    
    
        ...
        maven {
    
     url 'https://jitpack.io' }
    }
}

dependencies {
    
    
    implementation 'com.github.tbruyelle:rxpermissions:0.10.2'
    implementation 'io.reactivex.rxjava2:rxjava:2.2.6'
    implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
}

Code

package com.fenghongzhang.rxpermission;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.os.Bundle;
import android.util.Log;


import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.functions.Consumer;


public class MainActivity extends AppCompatActivity {
    
    
    final RxPermissions rxPermissions = new RxPermissions(this);

    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //单个授权
        rxPermissions.request(new String[]{
    
    Manifest.permission.CALL_PHONE})
                    .subscribe(new Consumer<Boolean>() {
    
    
                        @Override
                        public void accept(Boolean aBoolean) {
    
    
                            Log.i(TAG, "accept: "+aBoolean);
                        }
                    });


//        //多个授权
//         rxPermissions.requestEach(new String[]{Manifest.permission.CALL_PHONE,Manifest.permission.READ_SMS})
//                 .subscribe(new Consumer<Permission>() {
    
    
//                     @Override
//                     public void accept(Permission permission) {
    
    
//                         if(permission.granted){
    
    
//                             Log.i(TAG, "accept: 同意");
//                         }else if(permission.shouldShowRequestPermissionRationale){
    
    
//                             Log.i(TAG, "accept: 不同意");
//                         }else{
    
    
//                             Log.i(TAG, "accept: 一直不同意");
//                         }
//                     }
//                 });

    }
}

RLifeCycle

https://github.com/trello/RxLifecycle

One: The role
of Rxlifecycle With the popularity of the third library of Android, RxJava (https://github.com/search?q=RxJava) and RxAndroid (https://github.com/ReactiveX/RxAndroid) are becoming more and more popular Familiar, concise grammar, combined with Java8 Lambda expressions, make the structure of the code clearer, easier to control and switch threads through the thread scheduler, various advantages, more and more people use it. But poor use can easily lead to memory leaks.

Rxlifecycle (https://github.com/trello/RxLifecycle) is used to strictly control the memory leak caused by the inability to destroy the Activity/Fragment due to the release of a subscription and the failure to cancel it in time.

For the specific version to be used, you can find the corresponding version in the corresponding GitHub.

2: Rxlifecycle uses
Activity/Fragment to inherit RxAppCompatActivity/RxFragment, currently supported are RxAppCompatActivity, RxFragment, RxDialogFragment, RxFragmentActivity.

Guide package


    implementation 'com.trello.rxlifecycle2:rxlifecycle:2.2.1'
    implementation 'com.trello.rxlifecycle2:rxlifecycle-android:2.2.1'
    implementation 'com.trello.rxlifecycle2:rxlifecycle-components:2.2.1'

Pay attention to the output in the log. Add compose(bindUntilEvent(ActivityEvent.DESTROY)). When the activity is closed, the log will stop outputting, otherwise it will keep outputting and memory leaks.

Code

package com.fenghongzhang.rxpermission;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import com.trello.rxlifecycle2.android.ActivityEvent;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;


public class Main2Activity extends RxAppCompatActivity {
    
    

    private static final String TAG = "Main2Activity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        Observable.interval(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new Consumer<Object>() {
    
    
                    @Override
                    public void accept(Object o) throws Exception {
    
    
                        Log.i(TAG, "accept: ");
                    }
                });


    }
}

Guess you like

Origin blog.csdn.net/shuai_ge_feng/article/details/113767699