RxJava/RxAndroid:ConnectableObservable & replay(int bufferSize, long time, TimeUnit unit)

简介: RxJava/RxAndroid:ConnectableObservable & replay(int bufferSize, long time, TimeUnit unit)import android.

RxJava/RxAndroid:ConnectableObservable & replay(int bufferSize, long time, TimeUnit unit)

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.observables.ConnectableObservable;
import io.reactivex.observers.DisposableObserver;

public class MainActivity extends AppCompatActivity {
    private String TAG = "输出";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ConnectableObservable<Long> mConnectableObservable = Observable
                .interval(1, TimeUnit.SECONDS)
                .replay(2, 5, TimeUnit.SECONDS);

        mConnectableObservable
                .subscribe(new DisposableObserver<Long>() {
                    @Override
                    public void onNext(Long along) {
                        Log.d(TAG, "onNext1:" + along);
                    }

                    @Override
                    public void onComplete() {
                        Log.d(TAG, "onComplete1");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d(TAG, e.toString());
                    }
                });

        mConnectableObservable.connect();

        mConnectableObservable
                .delaySubscription(5, TimeUnit.SECONDS)
                .subscribe(new DisposableObserver<Long>() {
                    @Override
                    public void onNext(Long along) {
                        Log.d(TAG, "onNext2:" + along);
                    }

                    @Override
                    public void onComplete() {
                        Log.d(TAG, "onComplete2");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d(TAG, e.toString());
                    }
                });
    }
}


output:

01-26 16:18:45.442 21669-21690/zhangphil.book D/输出: onNext1:0
01-26 16:18:46.442 21669-21690/zhangphil.book D/输出: onNext1:1
01-26 16:18:47.442 21669-21690/zhangphil.book D/输出: onNext1:2
01-26 16:18:48.442 21669-21690/zhangphil.book D/输出: onNext1:3
01-26 16:18:49.442 21669-21690/zhangphil.book D/输出: onNext1:4
01-26 16:18:49.444 21669-21691/zhangphil.book D/输出: onNext2:3
01-26 16:18:49.444 21669-21691/zhangphil.book D/输出: onNext2:4
01-26 16:18:50.442 21669-21690/zhangphil.book D/输出: onNext1:5
01-26 16:18:50.442 21669-21690/zhangphil.book D/输出: onNext2:5
01-26 16:18:51.442 21669-21690/zhangphil.book D/输出: onNext1:6
01-26 16:18:51.442 21669-21690/zhangphil.book D/输出: onNext2:6
01-26 16:18:52.442 21669-21690/zhangphil.book D/输出: onNext1:7
01-26 16:18:52.442 21669-21690/zhangphil.book D/输出: onNext2:7
01-26 16:18:53.442 21669-21690/zhangphil.book D/输出: onNext1:8
01-26 16:18:53.442 21669-21690/zhangphil.book D/输出: onNext2:8
01-26 16:18:54.442 21669-21690/zhangphil.book D/输出: onNext1:9
01-26 16:18:54.442 21669-21690/zhangphil.book D/输出: onNext2:9
01-26 16:18:55.442 21669-21690/zhangphil.book D/输出: onNext1:10
01-26 16:18:55.442 21669-21690/zhangphil.book D/输出: onNext2:10
01-26 16:18:56.442 21669-21690/zhangphil.book D/输出: onNext1:11
01-26 16:18:56.442 21669-21690/zhangphil.book D/输出: onNext2:11
01-26 16:18:57.442 21669-21690/zhangphil.book D/输出: onNext1:12
01-26 16:18:57.442 21669-21690/zhangphil.book D/输出: onNext2:12

......


相关文章
|
druid 网络协议 Java
Spring Boot集成Druid异常discard long time none received connection.
Spring Boot集成Druid异常discard long time none received connection.
2766 0
|
6月前
|
存储 算法 物联网
int8与long long的深入对比与探讨
int8与long long的深入对比与探讨
|
6月前
|
存储 编译器 程序员
int 和 long 的区别
int 和 long 的区别
|
6月前
|
Java
Java中Long转Int转字符串Int转Long以及Int超出长度判断
Java中Long转Int转字符串Int转Long以及Int超出长度判断
63 0
|
编译器
unsigned long int 和 unsigned long一样吗?
unsigned 代表的是无符号的整形数
278 0
YOLO V5出现RuntimeError: result type Float can‘t be cast to the desired output type long int解决方法
YOLO V5出现RuntimeError: result type Float can‘t be cast to the desired output type long int解决方法
685 0
|
存储 Java
【剑指offer知识点】Java中无符号整型、如何与int/long类型互相转化
【剑指offer知识点】Java中无符号整型、如何与int/long类型互相转化
418 0
【剑指offer知识点】Java中无符号整型、如何与int/long类型互相转化
|
算法 Java 关系型数据库
如何更快地将string转换成int/long 下
如何更快地将string转换成int/long 下
|
存储 消息中间件 JavaScript
如何更快地将string转换成int/long 上
如何更快地将string转换成int/long 上
|
Java
java基本数据类型, byte: short: int: long: float: double: float和double有什么区别 boolean: ch
java基本数据类型, byte: short: int: long: float: double: float和double有什么区别 boolean: ch
267 0

热门文章

最新文章

下一篇
无影云桌面