1
我想在Android中構建一個啓動畫面。在加載啓動畫面時,圖像加載器會嘗試從服務器獲取較新的啓動畫面,然後較新的圖像將被保存到本地存儲器,並用作下次啓動的啓動畫面圖像。啓動屏幕將保持5秒,然後跳轉到主要活動。我們還需要啓動畫面上的倒數計數器。使用RxJava在複雜的條件下鏈式觀察值
我能夠使用Observable.interval來獲取啓動屏幕在完成時消失並更新下一個發射計數器。但是我應該在計數器啓動時加載圖像,並且圖像檢索任務的超時也是5秒,如果我們無法獲取圖像,那麼取消任務?
splash_sub = Observable
.interval(SPLASH_INTERVAL, TimeUnit.SECONDS) //to emit event every 1s
.take(SPLASH_DURATION) //limit the event number to 5, i.e. 5s to complete
.subscribeOn(Schedulers.newThread()) //timer and event emitting run in background
.observeOn(AndroidSchedulers.mainThread()) //subscriber callback run in UI thread
.subscribe(new Observer<Long>() {
@Override
public void onCompleted() {
dismissSplash(); //dismiss the Splash Screen and enter the Main Activity
}
@Override
public void onError(Throwable e) {
Toast.makeText(getApplicationContext(), "Error!", Toast.LENGTH_SHORT).show();
}
@Override
public void onNext(Long aLong) {
Log.d(TAG, aLong.toString());
textView.setText(String.format("%s", aLong));
}
});