Mark023 Mark023 - 4 months ago 19
Java Question

Countdown Time in Android

I am developing an Android app and I have a requirement to:

1) Countdown time from

'x' minutes
to
'1' minute
and then once it is at
1 minute
, count in terms of
60 sec, 30 sec
and then
0
.

I am counting down in terms of 1 min(60000).

My code is:

public void countdowntimer(long timeinmillis, long countdowninterval){
Log.d("hi","Iamhere 0" + timeinmillis/60000);
new CountDownTimer(timeinmillis, countdowninterval) {

public void onTick(long timeinmillis) {


//Countdown the time in terms of minutes

Log.d("hi","Iamhere 2" + timeinmillis);
tv.setText(String.valueOf((timeinmillis)/60000) + "min");
rootView.invalidate();
if(timeinmillis <= 60000 && timeinmillis > 30000){
tv.setText(String.valueOf(60) + "sec");
rootView.invalidate();
}else if(timeinmillis < 30000){
tv.setText(String.valueOf(30) + "sec");
rootView.invalidate();
}
}

public void onFinish() {

}
}.start();
}


The logs are:

Iamhere0 4

Iamhere1 3


Why is my second log showing 1 minute lesser than first log and how do I implement 60sec, 30sec countdown once it is at 1 minute?

The expected output is:

4 min

3 min

2 min

1 min -> 60sec

30sec

0

Answer

Use AtomicInteger to maintain the threads condition.

In First Condition its display time in minute

 if (min.get() > 1) {
                        mCount.setText(Integer.toString(min.get())+" min");
                        handler.postDelayed(this, 1000);
                        min.getAndDecrement();
                    }

AND

In Last one minute its goes to second condition which display time in seconds

else {

                    if(min.get()== 1) {
                        mCount.setText("60 Sec");
                        handler.postDelayed(this, 500);
                    }
                    else if(min.get() == 0){
                        mCount.setText("30 Sec");
                        handler.postDelayed(this, 500);
                    }
                    else
                        mCount.setText("0 Sec");
                        min.getAndDecrement();
                }

Use this ...........

final TextView mCount = (TextView) findViewById(R.id.count);
        final Handler handler = new Handler();
        final AtomicInteger min = new AtomicInteger(4);
        final Runnable counter = new Runnable() {
            @Override
            public void run() {

                if (min.get() > 1) {
                    mCount.setText(Integer.toString(min.get())+" min");
                    handler.postDelayed(this, 1000);
                    min.getAndDecrement();
                } else {

                    if(min.get()== 1) {
                        mCount.setText("60 Sec");
                        handler.postDelayed(this, 500);
                    }
                    else if(min.get() == 0){
                        mCount.setText("30 Sec");
                        handler.postDelayed(this, 500);
                    }
                    else
                        mCount.setText("0 Sec");
                        min.getAndDecrement();
                }

            }
        };
        handler.postDelayed(counter, 0);

enjoy coding.....