Domanda

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.

È stato utile?

Soluzione

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(); 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top