simple rxjava code programming style

简介: simple rxjava code programming stylepackage zhangphil.rx;import android.

simple rxjava code programming style


package zhangphil.rx;

import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.util.concurrent.Callable;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {

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

        DisposableObserver mDisposableObserver = new DisposableObserver<String>() {
            @Override
            public void onNext(String s) {
                text.setText(s);
            }

            @Override
            public void onComplete() {

            }

            @Override
            public void onError(Throwable e) {

            }
        };

        Observable.fromCallable(new Callable<String>() {
            @Override
            public String call() throws Exception {
                SystemClock.sleep(10000);

                return "zhang phil";
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(mDisposableObserver);
    }
}


in app's gradle,need ref:

    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
    compile 'io.reactivex.rxjava2:rxjava:2.1.8'

相关文章
|
缓存 监控 架构师
Go 函数式编程:Higher-order function
Go 函数式编程:Higher-order function
132 0
|
Web App开发 JavaScript 前端开发
Selenium使用中报错:We\'re sorry but hr-frontend-v2 doesn\'t work properly without JavaScript enabled
Selenium使用中报错:We\'re sorry but hr-frontend-v2 doesn\'t work properly without JavaScript enabled. Please enable it to continue 这个错误提示表明目标网页要求启用JavaScript才能正常工作,而默认情况下,Selenium WebDriver是启用JavaScript的。如果遇到此错误,请按照以下步骤尝试解决问题
742 0
Selenium使用中报错:We\'re sorry but hr-frontend-v2 doesn\'t work properly without JavaScript enabled
|
弹性计算 Linux Docker
|
Android开发 Kotlin
kotlin协程库报错“Program type already present”解决
最近在学习kotlin,学习到协程库这一块了,针对Android的话就是coroutines-android库。本来学习就不容易了,再加上kotlin现在还处于快速变化期,那个酸爽简直了,废话不多说,进入正题。
|
Java 编译器
The amazing thing about Java
The amazing thing about Java
|
API Kotlin
RxJava的Single、Completable以及Maybe
RxJava的Single、Completable以及Maybe
278 0
RxJava的Single、Completable以及Maybe
|
iOS开发 开发者 C++
Effective Objective-C 2.0 Tips 总结 Chapter 5,6,7
Effective Objective-C 2.0 Tips 总结 Chapter 5,6,7 Chapter 5 内存管理 Tips 29 理解引用计数 引用计数是 Objective-C 内存管理的基础,包括 ARC 也是建立在引用计数的基础之...
1290 0
|
JavaScript 前端开发 Ruby