Most Obsersables
are passive by default and they will only emit items if they are subscribed to. In your code example you're not subscribing to your Observable
. Therefore it never actually starts emitting items or in this case the only items after 2000 milliseconds.
flatMap
is simply an Operator
to help manipulate the stream of data but it doesn't subscribe to your stream of Observables
.
Instead of using flatMap
you should replace it with a call to subscribe
.
Observable.timer(2000, TimeUnit.MILLISECONDS)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Long>() {
@Override
public void call(Long aLong) {
continuePlayback();
}
});
I used the Action
instead of an Observer
in the subscribe
since I don't believe you'll need to handle onError
in this particular case.
The Observable
from timer
will complete after emitting it's only item.
Keep in mind that if you're using Observables
from within a Fragment
or an Activity
you should always make sure you unsubscribe
from your Observables
to eliminate chances of memory leaks.
Here's a quick link to the Hot and Cold Observables in RxJava
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…