一、如何使用

在build.gradle中添加依赖

 dependencies {
api 'io.reactivex:rxandroid:1.2.1'
api 'io.reactivex:rxjava:1.3.0'
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

这里有一个小坑,直接用latest.release没有办法用,不知道为什么

二、代码实现

2.1 使用just+Action1+Action0来实现

  Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
.subscribeOn(Schedulers.io()) // Observable运行的线程
.observeOn(AndroidSchedulers.mainThread()) //监听者运行的线程
.subscribe(new Action1<Integer>() {
//onNext
@Override
public void call(Integer integer) {
log("call 1:" + integer);
}
}, new Action1<Throwable>() {
//onError
@Override
public void call(Throwable throwable) {
log("call 2");
}
}, new Action0() {
//onCompleted
@Override
public void call() {
log("call 3");
}
});

运行结果如下,很简单,就不一一解释了。

 10-05 11:05:45.955 23619 23619 E MainActivity: yanlog msg:call 1:1
10-05 11:05:45.955 23619 23619 E MainActivity: yanlog msg:call 1:2
10-05 11:05:45.955 23619 23619 E MainActivity: yanlog msg:call 1:3
10-05 11:05:45.955 23619 23619 E MainActivity: yanlog msg:call 1:4
10-05 11:05:45.955 23619 23619 E MainActivity: yanlog msg:call 1:5
10-05 11:05:45.955 23619 23619 E MainActivity: yanlog msg:call 1:6
10-05 11:05:45.955 23619 23619 E MainActivity: yanlog msg:call 1:7
10-05 11:05:45.955 23619 23619 E MainActivity: yanlog msg:call 1:8
10-05 11:05:45.956 23619 23619 E MainActivity: yanlog msg:call 1:9
10-05 11:05:45.956 23619 23619 E MainActivity: yanlog msg:call 1:10
10-05 11:05:45.956 23619 23619 E MainActivity: yanlog msg:call 3

2.2 使用Just+Subscriber来实现

代码如下:

            Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
.subscribeOn(Schedulers.io()) // Observable运行的线程
.observeOn(AndroidSchedulers.mainThread()) //监听者运行的线程
.subscribe(new Subscriber<Integer>() {
@Override
public void onCompleted() {
log("onCompleted");
} @Override
public void onError(Throwable e) {
log("onError");
} @Override
public void onNext(Integer integer) {
log("onNext:" + integer);
}
});

运行结果如下:

 10-05 19:56:09.991   982   982 E MainActivity: yanlog msg:onNext:1
10-05 19:56:09.991 982 982 E MainActivity: yanlog msg:onNext:2
10-05 19:56:09.991 982 982 E MainActivity: yanlog msg:onNext:3
10-05 19:56:09.992 982 982 E MainActivity: yanlog msg:onNext:4
10-05 19:56:09.992 982 982 E MainActivity: yanlog msg:onNext:5
10-05 19:56:09.992 982 982 E MainActivity: yanlog msg:onNext:6
10-05 19:56:09.992 982 982 E MainActivity: yanlog msg:onNext:7
10-05 19:56:09.992 982 982 E MainActivity: yanlog msg:onNext:8
10-05 19:56:09.992 982 982 E MainActivity: yanlog msg:onNext:9
10-05 19:56:09.992 982 982 E MainActivity: yanlog msg:onNext:10
10-05 19:56:09.992 982 982 E MainActivity: yanlog msg:onCompleted

Android RxJava小结的更多相关文章

  1. Android RxJava 2 的用法 just 、from、map、subscribe、flatmap、Flowable、Function、Consumer ...【转】

    先简单说说RxJava的用途与价值 原文出处:Android RxJava 2 的用法 用途: 异步 (也就是开线程跳转) 价值: 面对复杂的逻辑,它依然 简洁 ,代码 易读 RxJava2 与 Rx ...

  2. android基础小结

    (注:此小结文档在全屏模式下观看效果最佳) 2016年3月1日,正式开始了我的android学习之路. 最最开始的,当然是学习怎样搭载环境了,然而苦逼的我在win10各种坑爹的指引下还是安装了一个星期 ...

  3. 深入Android RxJava 2

    这篇文章是根据Jake Wharton在GOTO CopenHagen 2016上的讲话整理的. 下一个版本(2.0)的RxJava还在开发中.虽然observable.订阅管理和背压(backpre ...

  4. Android RxJava

    GitHut 地址: https://github.com/ReactiveX/RxAndroid build.gradle: compile 'io.reactivex:rxandroid:1.2. ...

  5. Android RxJava+Retrofit完美封装

    作者简介 本篇来自 小河马 的投稿,分享了自己是如何进行 RxJava+Retrofit 的封装.本文的技术点自然没话说,另外对于这种模块化的思路,希望能帮助到大家.最后提前祝大家周末愉快以及圣诞快乐 ...

  6. android 知识小结-1

    Java哪些数据结构是线程安全的,CurrentHashMap的原理 ConcurrentHashMap.ConcurrentSkipListMap.ConcurrentSkipListSet.Con ...

  7. Android技巧小结之新旧版本Notification

    最近开发用到了通知功能,但有几个地方老是提示deprecated,然后就找了篇文章学习了下新旧版本的不同. Notification即通知,用于在通知栏显示提示信息. 在较新的版本中(API leve ...

  8. Android RxJava/RxAndroid结合Retrofit使用

    概述 RxJava是一个在 Java VM 上使用可观測的序列来组成异步的.基于事件的程序的库.更重要的是:使用RxJava在代码逻辑上会非常简洁明了,尤其是在复杂的逻辑上.告别迷之缩进. RxAnd ...

  9. Android - Rxjava 使用和原理

    用RxJava写的一个Android的小Demo 我所理解的RxJava——上手其实很简单 http://www.jianshu.com/p/5e93c9101dc5

随机推荐

  1. SPOJ:Divisors of factorial (hard) (唯一分解&分块优化)

    Factorial numbers are getting big very soon, you'll have to compute the number of divisors of such h ...

  2. [SoapUI] Read data from response , use it to update parameter

    import com.eviware.soapui.support.GroovyUtils def groovyUtils = new GroovyUtils( context ) def holde ...

  3. ES6之拷贝对象

    function copyObject(orig) { var copy = Object.create(Object.getPrototypeOf(orig)); //创建一个新的原型对象 copy ...

  4. node安装升级npm

    安装npm npm上有很多优秀的nodejs包,来解决常见的一些问题,比如用node-mysql,就可以方便通过nodejs链接到mysql,进行数据库的操作 在开发过程往往会需要用到其他的包,使用n ...

  5. Task运行带参数的函数

    Task<Int32>   task = Task.Run(() => fun("s", 9)); 函数定义: private Int32 frun(string ...

  6. 6-8 adaboost分类器2

    重点分析了Adaboost它的分类结构,以及如何使用Adaboost.这一节课讲解Adaboost分类器它训练的步骤以及训练好之后的XML文件的文件结构.所以这节课的核心是Adaboost分类器它的训 ...

  7. sql server2008配置管理工具服务显示远程过程调用失败

    SQL SERVER2008配置管理工具服务显示远程过程调用失败   前两天,装了VS2012后,打开SQL2008配置管理工具,发现SQL服务名称里什么也没有,只有一个提示:(如图) 上网搜了,试了 ...

  8. TP5之安全机制

    防止sql注入 1.查询条件尽量使用数组方式,具体如下: $wheres = array(); $wheres['account'] = $account; $wheres['password'] = ...

  9. UVALive 6833【模拟】

    题意: 算从左往右的值,先乘后加的值,数的范围<=9= =,然后根据满足的条件输出字符. 思路: 从左往右就是直接来了,先做乘法就是乘法两边的数字靠向右边那个,且左边那个为0,然后所有值一加就好 ...

  10. 51nod 1050【DP】

    思路: 就是先正常的dp一下求一个最大连续子串,然后特殊情况就是sum-最小连续子串.. 比一比谁大谁小就好了 #include <stdio.h> #include <string ...