Getting started with RxJava2 - installation and simple examples (1)

Keywords: Android Gradle xml encoding

In Android development, you need to transfer the data in the Server and its team threads to the text box. It's not very convenient to use the broadcast or handler. Once you find an RxJava on the Internet, you can learn it.

1, Add RxJava2 to Android Studio

1. Add in build.Gradle:
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'io.reactivex.rxjava2:rxjava:2.1.2'
2. Click synet,
3. Add complete.

2, Simple use of RxJava2

1. Set a scene.
There is a fast food restaurant.
You ordered a fast food and left your address.
Send meals to your house.

2. Write code.

      //Create a fast food restaurant. Observable (observed)
      Observable<String> kuaicandian=Observable.create(new ObservableOnSubscribe<String>() {
          @Override
          public void subscribe(ObservableEmitter<String> e) throws Exception {
              Thread.sleep(6000);
              e.onNext("Fast food (beef noodles)");
              e.onComplete();
          }
      });
        //Create a you. Observer
        Observer<String> observer = new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                Log.d("RxJava-Start delivering fast food", "subscribe");
            }

            @Override
            public void onNext(String value) {
                Log.d("RxJava-Fast food service", "" + value);
            }

            @Override
            public void onError(Throwable e) {
                Log.d("RxJava-Fast food delivery error", "error");
            }

            @Override
            public void onComplete() {
                Log.d("RxJava-Fast food delivery this task is completed", "complete");
            }
        };
        //Leave your address with the fast food restaurant. Subscribe
        observable.subscribe(observer);

3. Click "key 1", the interface is frozen (beef noodle is being made). After a while, the operation result appears

There is no error, so there are only three lines of information.

4. All codes are as follows:
Interface code:

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         xmlns:tools="http://schemas.android.com/tools"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         tools:context="com.example.ls30_rxjava2_1.MainActivity">


         <TextView
         android:id="@+id/tv"
         android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:text="TextView" />

     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:gravity="bottom"
         android:orientation="horizontal">

         <Button
             android:id="@+id/bt1"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:text="Key 1"
             android:onClick="bt1_onClick"
             />

         <Button
             android:id="@+id/bt2"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:text="Key 2"
             android:onClick="bt2_onClick"/>

         <Button
             android:id="@+id/bt3"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:text="Key 3"
             android:onClick="bt3_onClick"/>

         <Button
             android:id="@+id/bt4"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:text="Key 4"
             android:onClick="bt4_onClick"/>
     </LinearLayout>
     </RelativeLayout>

java code

public class MainActivity extends AppCompatActivity {

  TextView tv;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      tv=(TextView) findViewById(R.id.tv);
  }

  public void bt1_onClick(View view) {
      tv.setText("");
      //Create a fast food restaurant. Observable (observed)
      Observable<String> kuaicandian=Observable.create(new ObservableOnSubscribe<String>() {
          @Override
          public void subscribe(ObservableEmitter<String> e) throws Exception {
              Thread.sleep(6000);
              e.onNext("Fast food (beef noodles)");
              e.onComplete();
          }
      });

      Observer<String> wo=new Observer<String>() {
          @Override
          public void onSubscribe(Disposable d) {
              tv.setText(tv.getText()+"RxJava-Start delivering fast food"+"\n");
          }

          @Override
          public void onNext(String s) {
              tv.setText(tv.getText()+"RxJava-Fast food service:"+s+"\n");
          }

          @Override
          public void onError(Throwable e) {
              tv.setText(tv.getText()+"RxJava-Fast food delivery error"+"\n");
          }

          @Override
          public void onComplete() {
              tv.setText(tv.getText()+"RxJava-The fast food is delivered"+"\n");
          }
      };
      //Leave your address with the fast food restaurant. Subscribe
      kuaicandian.subscribe(wo);

  }
  public void bt3_onClick(View view) {
      tv.setText("Press button 3");
  }
    public void bt2_onClick(View view) {
      tv.setText("Press button 2");
  }
  public void bt4_onClick(View view) {
      tv.setText("Click button 4");
  }
}

Posted by skateme on Fri, 03 Apr 2020 04:03:49 -0700