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'