How to set timer in android?

后端 未结 21 914
渐次进展
渐次进展 2020-11-22 00:51

Can someone give a simple example of updating a textfield every second or so?

I want to make a flying ball and need to calculate/update the ball coordinates every se

21条回答
  •  别跟我提以往
    2020-11-22 01:46

    I think you can do it in Rx way like:

     timerSubscribe = Observable.interval(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1() {
                    @Override
                    public void call(Long aLong) {
                          //TODO do your stuff
                    }
                });
    

    And cancel this like:

    timerSubscribe.unsubscribe();
    

    Rx Timer http://reactivex.io/documentation/operators/timer.html

提交回复
热议问题