Shivangi
Shivangi

Reputation: 91

How to implement two countdown timers alternatively in Android

I have implemented one counter and onFinish() of first counter,I started second counter but the first counter not able to finish.Text "Bye Guyz" remain for some time so how to finish the text.

Please help me.

Thanks in advance.!!!

Code :-

counter= new CountDownTimer(10000, 1000) {

                public void onTick(long millisUntilFinished) {
                    if (count == 0) {
                        tv.setText("First counter");
                        tv2.setVisibility(View.VISIBLE);
                        tv2.setText("Hello Guyz");
                    }
                }

                public void onFinish() {
                    if(!flag) {
                        tv2.setText("Bye Guyz");
                        count = 0;
                        try {
                            counter.cancel();
                        }catch (Exception e){}
                    }
                    else if(flag) {
                       counter1 = new CountDownTimer(9000, 1000) {
                            public void onTick(long millisUntilFinished) {
                                flag = false;
                                tv.setText("Second counter");
                        tv2.setVisibility(View.VISIBLE);
                        tv2.setText("Hello Girls");
                                count = 0;
                            }

                            public void onFinish() {
                                tv2.setVisibility(View.VISIBLE);
                                tv2.setText("Bye Girls");
                                count = 0;
                            }
                        }.start();

Upvotes: 1

Views: 186

Answers (3)

Emmanuel Mtali
Emmanuel Mtali

Reputation: 4963

Here is my alternative is as follows

Create the custom Counterextending Thread

class Counter extends Thread {
    private long timeOne, timeTwo;
    private OnCounterFinishedListener mCounterFinishedListener;
    private Thread t;
    Activity activity = null;


    Counter(Context context){
        t = new Thread(this);
        activity = (Activity)context;
    }

    @Override
    public void run() {
        try {
            sleep(timeOne);
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mCounterFinishedListener.firstCounterFinished();
                }
            });

            sleep(timeTwo);
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mCounterFinishedListener.secondCounterFinished();
                }
            });

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

     void setTimes(long timeOne, long timeTwo){
        this.timeOne = timeOne;
        this.timeTwo = timeTwo;
    }

    public void start(OnCounterFinishedListener listener){
        mCounterFinishedListener = listener;
        t.start();
    }




    interface OnCounterFinishedListener{
        void firstCounterFinished();
        void secondCounterFinished();
    }
}

Then inside your main thread you can start this counter as

final Counter counter = new Counter(this);
counter.setTimes(5000, 5000);
counter.start(new Counter.OnCounterFinishedListener() {
    @Override
    public void firstCounterFinished() {
        // Update your first TextView
    }

    @Override
    public void secondCounterFinished() {
        // Update your second TextView
    }
});

Upvotes: 0

Raphau
Raphau

Reputation: 126

If you say your tv2 displays "Bye Guyz" it means that your flag is set to false, so the "else if" part is not being executed. onFinish() is only executed once, so you need to make sure the flag is set for true to start the second counter.

Also you shouldn't cancel your counter in onFinish() because it's already finished.

Upvotes: 0

zapotec
zapotec

Reputation: 2638

  • Did you "debug" the code to be sure the code is arriving to counter1 = new CountDownTimer(9000, 1000)?

  • Are you sure when the first counter arrives to onFinish() the flag variable is true?

  • Why do you call counter.cancel() in onFinish() when obviously the counter is already over?

public void onFinish() { if(!flag) { tv2.setText("Bye Guyz"); count = 0; try { counter.cancel(); }catch (Exception e){} }

Upvotes: 1

Related Questions