Skip to content

Instantly share code, notes, and snippets.

@lenamuit
Last active October 31, 2019 09:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lenamuit/fe4fc55152d6596a1ae6ec83c13483c6 to your computer and use it in GitHub Desktop.
Save lenamuit/fe4fc55152d6596a1ae6ec83c13483c6 to your computer and use it in GitHub Desktop.
Java, Android, RxJava. Fastest and simplest way to build a countdown timer or stick timer

In the pass, to build the timer task, for example: to show count down timer on TextView or show current position when play the music or video use MediaPlayer, I use TimerTask or Timer object as the best choices.

This is the old source code

timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
    @Override 
    public void run() { 
        runOnUiThread(new Runnable() {
            @Override 
            public void run() { 
                if (isTrue()) { 
                    tv.post(new Runnable() {
                        @Override 
                        public void run() { 
                            tv.setText(getTimerText()); 
                        } 
                    }); 
                } 
            } 
        }); 
    } 
}, 0, 1000); 

**Above solution is rarely basically, It's not good in some cases. **

Now, I will show you how to solve above task with rxjava

int mSickTimer = 0;
//build the Observable object
final Observable<Integer> timerObservable = Observable.create(new Observable.OnSubscribe<Integer>() {
  @Override
  public void call(Subscriber<? super Integer> subscriber) {
      subscriber.onStart();
      while (isTrue()) {
          subscriber.onNext(mSickTimer ++);
          try {
              Thread.sleep(1000);
          } catch (InterruptedException e) {
              subscriber.onCompleted();
          }
      }
      subscriber.onCompleted();
  }
});
//Excute the observable by call *subscribe*, 
// Key features here, you can control what thread excute timer and handle callback. 
timerObservable.subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(timerObserver);
    
//timerObserver
timerObserver = new Observer<Integer>() {
            @Override
            public void onCompleted() {
                //complete timer
            }

            @Override
            public void onError(Throwable e) {
                //error -> I will make it finish here
            }

            @Override
            public void onNext(Integer i) {
                updateTimer(i);
            }
        }

If you're interested with RxJava, you can try and give me your opinion.

Thanks!

@jemshit
Copy link

jemshit commented Aug 20, 2016

this makes thread to sleep i guess

@mibrahimdev
Copy link

why don't you just use Observable.intervl() or Observable.timer() ?

@sunzcdev
Copy link

sunzcdev commented May 4, 2018

把简单的问题搞复杂,这样看起来会很牛逼

@mabuthraa
Copy link

把简单的问题搞复杂,这样看起来会很牛逼

哈哈,你是对的👍🏼

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment