Skip to content Skip to sidebar Skip to footer

Android Countdown Timer Cancel Vs Timeup

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

Solution 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(); 

Post a Comment for "Android Countdown Timer Cancel Vs Timeup"