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

CountDownTimercountDownTimerFixed=newCountDownTimer(Time, Tick) {

    @OverridepublicvoidonTick(long millisUntilFinished) {

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

    }

    @OverridepublicvoidonFinish() {
        // 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 = newCountDownTimer(Timer, Tick) {

                @OverridepublicvoidonTick(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);
                }

                @OverridepublicvoidonFinish() {
                    // 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"