【发布时间】:2018-03-20 08:08:39
【问题描述】:
我正在尝试使用 RxJava 在加载某些方法期间显示 AlertDialog。它不起作用,UI 被阻塞了 2 秒,当使用 Debugger 单步执行它时,调试器显示它在 UI 线程上运行。我添加了 Schedulers.IO,我做错了什么?
boolean initialize() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
return true;
}
public AlertDialog showSomePopup(Context context, String msg) {
return new AlertDialog.Builder(context)
.setTitle("Loading...")
.setMessage(msg)
.setPositiveButton("Ok", null)
.show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final AlertDialog dialog = showSomePopup(this, "Waiting ..");
Single.just(initialize())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Boolean>() {
@Override
public void accept(@NonNull Boolean aBoolean) throws Exception {
dialog.dismiss();
}
});
}
【问题讨论】:
-
使用 Observable.interval(2000, TimeUnit.SECONDS) 代替 Single.just
标签: android multithreading rx-java