Android game countdown timer

感情迁移 提交于 2019-12-06 07:57:07

问题


I'm working on a game where I need a countdown timer.

I need to be able to pause the timer, resume the countdown and to add some time to current countdown state.

I've looked at CountdownTimer class and its methods, but it seems like it doesn't have the required features.

I need advice - which component is best for this case?

How to use it?

What are the possible problems?

Thread? AsyncTask? Timer?

Does anyone have experience with this?


回答1:


I think Thread can be used, but its easier to implement your features using CountdownTimer wrapper class:

    static class MyCountdownTimer {

    long mCurrentMilisLeft;
    long mInterval;
    CountdownTimerWrapper mCountdownTimer;

    class CountdownTimerWrapper extends CountDownTimer{
        public CountdownTimerWrapper(long millisInFuture,long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {

        }

        @Override
        public void onTick(long millisUntilFinished) {
            mCurrentMilisLeft = millisUntilFinished;
        }

    }

    public MyCountdownTimer(long millisInFuture, long countDownInterval) {          
        set(millisInFuture,countDownInterval);
    }

    public void pause(){
        mCountdownTimer.cancel();
    }

    public void resume(){
        mCountdownTimer = new CountdownTimerWrapper(mCurrentMilisLeft, mInterval);
        mCountdownTimer.start();
    }

    public void start(){
        mCountdownTimer.start();
    }

    public void set(long millisInFuture, long countDownInterval){
        mInterval = countDownInterval;
        mCurrentMilisLeft = millisInFuture;         
        mCountdownTimer = new CountdownTimerWrapper(millisInFuture, countDownInterval);
    }

}


来源:https://stackoverflow.com/questions/18041675/android-game-countdown-timer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!