Incrementing the countDownTimer Android

微笑、不失礼 提交于 2019-12-02 09:53:29

You should know that you don't have to manually call onFinish when time is up in the countdown class, neither do you have you to cancel in onFinish as one leads to the other or manually decrement anything. Try the following and see if it fits the task that you had in mind. Hope you can note that i don't see pragmatic reason to see if a button "isn't being clicked anymore".

public class MainActivity extends Activity {

    Button incrementTime, startTime;
    public TextView timedisplay;
    long millisInFuture = 1000;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        incrementTime = (Button) findViewById(R.id.button2);
        startTime = (Button) findViewById(R.id.button3);
        timedisplay = (TextView) findViewById(R.id.mycounter);

        resetText();

        incrementTime.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                millisInFuture += 1000;
                resetText();
            }
        });

        startTime.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                CountDownTimer wavetimer = new myTimer(millisInFuture + 3000, 1000).start();
                // ^ add 3 seconds.
            }
        });}

    protected void resetText() {
        timedisplay.setText("Time Left: " + millisInFuture / 1000);
    }

    public class myTimer extends CountDownTimer  {

        private long millisActual;

        public myTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
            millisActual = millisInFuture - 3000;
        }

        @Override
        public void onTick(long millisUntilFinished) {
            //v start showing the tick after 3 seconds.
            if (millisUntilFinished <= millisActual) {
                timedisplay.setText("Time Left: " + millisUntilFinished / 1000);                
            }
        }

        @Override
        public void onFinish() {
            timedisplay.setText("Countdown Finished");
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

Add the TextView with name 'timerText' in your activity xml file

Add following two global variables in the class

int repeatCounter=1;//incrementing for every 60 sec
CountDownTimer tripTimeCounter;

Add following method to the class & call it from activity onCreate or from any other place where you want

public void startTimeCounter(){
    tripTimeCounter=new CountDownTimer(60*1000, 1000){

        @Override
        public void onFinish() {
            // TODO Auto-generated method stub
            repeatCounter=repeatCounter+1;
            startTimeCounter();//follow the recursion on finish of the limit of 60 seconds & increment the repeat counter
        }

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

            timerText.setText(formatMillis((repeatCounter*60)*1000-millisUntilFinished));
        }

    }.start();
}

To show the value in the time format use following method

 static public String formatMillis(long val) {
    StringBuilder                       buf=new StringBuilder(20);
    String                              sgn="";

    if(val<0) { sgn="-"; val=Math.abs(val); }

    append(buf,sgn,0,( val/3600000));
    append(buf,":",2,((val%3600000)/60000));
    append(buf,":",2,((val %60000)/1000));
    //append(buf,".",3,( val%1000));
    return buf.toString();
 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!