android countdown timer cancel vs timeup

耗尽温柔 提交于 2019-12-13 06:49:29

问题


I am just wondering when timeout occurs, onFinish() method is called and we can execute further code there. But for some reason if timer is cancelled either manually or because of error does onFinish() get called? Or it just cancels the timer without calling any method.

Here are My two Counters.

Counter1:

    class Counter1 extends CountDownTimer {

    public Counter1(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onFinish() {
        // TODO Auto-generated method stub
        Log.d(TAG, " Timer1 Finished");
                    //Add 2min to second timer


    }

    @Override
    public void onTick(long millisUntilFinished) {
        // TODO Auto-generated method stub
                    /
    }
}

class Counter2 extends CountDownTimer {

    public Counter2(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        // TODO Auto-generated constructor stub
    }
    @Override
    public void onFinish() {
        // TODO Auto-generated method stub
        Log.d(TAG, "Timer2 Finished");          
    }
    @Override
    public void onTick(long millisUntilFinished) {
        // TODO Auto-generated method stub
    }
}

I have to add 2 min to second timer is its values is less than add 2minutes to its existing else it will continue with its timer.


回答1:


OnFinish() will not called at timer cancel you have to start it again, You can store time value in shared preference when application get crashed you have start time from saved time.

like

CountDownTimer countDownTimerFixed = new CountDownTimer(Time, Tick) {

    @Override
    public void onTick(long millisUntilFinished) {

        RTSharedPrefUtils.saveStringPrefernce(RTSharedPrefUtils.KEY_DOWNLOAD_TIMER, millisUntilFinished+ "");

    }

    @Override
    public void onFinish() {
        // TODO Auto-generated method stub

        RTSharedPrefUtils.saveStringPrefernce(RTSharedPrefUtils.KEY_DOWNLOAD_TIMER, 0 + "");
        this.start();

    }
};  

if timer is canceled,

Timer=Long.parseLong(RTSharedPrefUtils.fetchStringPrefernce(RTSharedPrefUtils.KEY_DOWNLOAD_TIMER,RTSharedPrefUtils.DEFAULT_DOWNLOAD_TIMER));

            countDownTimer = new CountDownTimer(Timer, Tick) {

                @Override
                public void onTick(long millisUntilFinished) {



                    RTSharedPrefUtils.saveStringPrefernce(
                            RTSharedPrefUtils.KEY_DOWNLOAD_TIMER,millisUntilFinished + "");
                    Log.d(TAG," Normalcount:\t"+ (Long.parseLong(RTSharedPrefUtils.fetchStringPrefernce(RTSharedPrefUtils.KEY_DOWNLOAD_TIMER,RTSharedPrefUtils.DEFAULT_DOWNLOAD_TIMER)))/ 1000);
                }

                @Override
                public void onFinish() {
                    // TODO Auto-generated method stub

                    }
                    RTSharedPrefUtils.saveStringPrefernce(
                            RTSharedPrefUtils.KEY_DOWNLOAD_TIMER, 0 + "");

                    countDownTimerFixed.start();

                }

};

countDownTimer.start(); 


来源:https://stackoverflow.com/questions/20782789/android-countdown-timer-cancel-vs-timeup

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