1

I have a Countdown timer and I want to return value when i call the class here is my class CountdownTimer

  public class CountDownTimerActivity extends CountDownTimer {
    public  boolean finish = false;
    public CountDownTimerActivity(long startTime, long interval) {
        super(startTime, interval);
    }

    @Override
    public  void onFinish()
    {
    finishTime(true);

    }

    @Override
    public void onTick(long millisUntilFinished) {
        finishTime(false);
       Log.e("TESTINg","" + millisUntilFinished/1000);
    }

    public boolean finishTime(boolean finish) {

        return finish;
    }
}

Here is my calling countdown timer

  CountDownTimerActivity countdowntimer  = new CountDownTimerActivity(5000,1000);
        countdowntimer.start();
        if(// I DONT KNOW WHAT WILL I PUT HERE)
        {
            Log.e("Testing", "OK na");
        }

Anyone can help me? Thank you

Jude Bautista
  • 160
  • 1
  • 16

4 Answers4

2

I think what you are trying to accomplish is a callback when the timer expires? If so, you have to think about the timer running by itself and then calling another method when finished. For instance:

public class Main extends Activity 
    {
    private MyCountDownTimer myCountDownTimer;

    @Override
    public void onCreate(Bundle savedInstanceState){
        myCountDownTimer = new MyCountDownTimer(5000, 1000);
        myCountDownTimer.start();
    }

    public void finished(){
        Log.e("Testing", "OK na");
    }

    public class MyCountDownTimer extends CountDownTimer {
        public MyCountDownTimer(long startTime, long interval) {
            super(startTime, interval);
        }

        @Override
        public void onFinish() { 
            finished();
        }

        @Override
        public void onTick(long millisUntilFinished) {
           Log.e("TESTINg","" + millisUntilFinished/1000);
        }
    }
}

See this link for more details/example: https://androidcookbook.com/Recipe.seam;?recipeId=1205

EDIT to make a universal class that can be used in other activities, I would do something like this.

Create a MyCountDownTimer class that looks like this in its own file:

    public class MyCountDownTimer extends CountDownTimer {
        private MyCallback myCallback;

        public MyCountDownTimer(long startTime, long interval) {
            super(startTime, interval);
        }

        public Interface MyCallBack{
            void callback();
        }

        @Override
        public void onFinish() { 
            myCallback.callback();
        }

        @Override
        public void onTick(long millisUntilFinished) {
           Log.e("TESTINg","" + millisUntilFinished/1000);
        }

        public void setMyCallback(MyCallback myCallback){
            this.myCallback = myCallback;
        }
    }

Then, in each of your activities, you would implement the new interface like so:

public class Main extends Activity implements MyCountDownTimer.MyCallback 
{
    private MyCountDownTimer myCountDownTimer;

    @Override
    public void callback(){
        Log.e("Testing", "OK na");
    }

    @Override
    public void onCreate(Bundle savedInstanceState){
        myCountDownTimer = new MyCountDownTimer(5000, 1000);
        myCountDownTimer.setMyCallback(this); //will use the callback method in this class which can be different for each activity
        myCountDownTimer.start();
    }
}
randal4
  • 590
  • 4
  • 16
  • Yes i get your point and I done that, but what i need is i want to create new class to call in entire codes because i always using countdown timer , – Jude Bautista Dec 10 '15 at 03:49
  • Did you get this working after the edit? Can you accept the answer if it helped you or let me know if you need more info. – randal4 Dec 10 '15 at 12:23
1

I don't think you can return a value from CountDownTimer. Ideally you should start the timer and implement what you have to do in the call back methods -

abstract void   onFinish()
//Callback fired when the time is up.

abstract void   onTick(long millisUntilFinished)
//Callback fired on regular interval.

sample implementation https://androidcookbook.com/Recipe.seam;jsessionid=DF53064E03C7505C4EBF727E56E0728E?recipeId=1205

shakhawat
  • 2,639
  • 1
  • 20
  • 36
1

CountDownTimer is a very simple class, you don't need to make an entire class for it, you could create it on your caller class and in that way you could call the callback function on finish.

public class MyActivity extends Activity{

    //Other methods and variables

    CountDownTimer countdowntimer  = new CountDownTimer(5000,1000){
        @Override
        public void onFinish() {
            //Call the callback from your activity
        }

        @Override
        public void onTick(long millisUntilFinished) {
            Log.e("TESTINg","" + millisUntilFinished/1000);
        }
    };

}

If for some reason you must to make it in a different class then in that class you must create an interface that your activity must implement and in your finish method call the method of the interface listener. Like in fragments: https://stackoverflow.com/a/34192088/2367237

Community
  • 1
  • 1
Gonzalo
  • 1,781
  • 15
  • 29
  • i dont understand your point in fragments i just want a class that will return the value of boolean when its done the counting – Jude Bautista Dec 10 '15 at 03:40
  • I said if you want a simple thing, define the `CountDownTimer` directly in your activity, like in the code. If you want something more complex and you need a different class you should follow the listener pattern, like in the example of the link, it's a fragment example but it's the same pattern. – Gonzalo Dec 10 '15 at 03:44
0

I think it's not working because you never set the variable finish to true on your code. You have to change your code in onFinish method to finish = true then put a logic inside onTick method to check if it's already finished then call onFinish() method;

Ruelos Joel
  • 2,209
  • 3
  • 19
  • 33