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
143 0
es6 basic intro
es6 basic intro
101 0
|
Java
解决bug:项目配置java8时出现Error:Jack is required to support java 8 language features...
解决bug:项目配置java8时出现Error:Jack is required to support java 8 language features...
338 0
解决bug:项目配置java8时出现Error:Jack is required to support java 8 language features...
PAT (Advanced Level) Practice - 1055 The World‘s Richest(25 分)
PAT (Advanced Level) Practice - 1055 The World‘s Richest(25 分)
118 0
|
Android开发 数据格式 XML
Architecture -- Data Binding Library
1). 简介 Data Binding Library是一个支持库,允许在布局文件中绑定数据源。最小支持API为14,gradle插件最小为1.5.0。示例 2).
903 0
|
Android开发
simple Android EventBus code
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.
721 0
|
JavaScript 前端开发 Ruby

热门文章

最新文章