Countdown timer how to implement onfinish method

I have a countdown timer that I want to implement using the finish method or some kind of code, so when the timer stops, the text views change to "Time Up" and another method is initiated (in action) . To clarify, the timer receives the start number, which counts from zero to zero in the format xx:xx.

Timer class:

public class countdown_timer {
private  long pls;
private long millisInFuture;
private long countDownInterval;
private boolean status;
public countdown_timer(long pMillisInFuture, long pCountDownInterval) {
    this.millisInFuture = pMillisInFuture;
    this.countDownInterval = pCountDownInterval;
    this.pls = pMillisInFuture;

    status = false;
    Initialize();
}

public void Stop() {
    status = false;
}
public  void Reset() {
    millisInFuture = pls;
}

public long getCurrentTime() {
    return millisInFuture;
}

public void Start() {
    status = true;
}
public void Initialize()
{
    final Handler handler = new Handler();
    Log.v("status", "starting");
    final Runnable counter = new Runnable(){

        public void run(){
            long sec = millisInFuture/1000;
            if(status) {
                if(millisInFuture <= 0) {
                    Log.v("status", "done");
                } else {
                    Log.v("status", Long.toString(sec) + " seconds remain");
                    millisInFuture -= countDownInterval;
                    handler.postDelayed(this, countDownInterval);
                }
            } else {
                Log.v("status", Long.toString(sec) + " seconds remain and timer has stopped!");
                handler.postDelayed(this, countDownInterval);
            }
        }
    };

    handler.postDelayed(counter, countDownInterval);
}

Activity used by the timer:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_card_game_2);
    //...find views
    mycounterup = new countdown_timer(startcard, 1000);
    mycounterdown = new countdown_timer(startcard, 1000);
    RefreshTimer();
    mycounterdown.Start();

  public void RefreshTimer()
{
    final Handler handler = new Handler();
    final Runnable counter = new Runnable(){

        public void run(){
            int minutes_up_start = (int) (mycounterup.getCurrentTime() / 1000) / 60;
            int seconds_up_start = (int) (mycounterup.getCurrentTime() / 1000) % 60;
            String time_2_up_start_formatted = String.format(Locale.getDefault(), "%02d:%02d", minutes_up_start, seconds_up_start);
            card_2_up.setText(time_2_up_start_formatted);

            int minutes_down_start = (int) (mycounterdown.getCurrentTime() / 1000) / 60;
            int seconds_down_start = (int) (mycounterdown.getCurrentTime() / 1000) % 60;
            String card_2_down_start_formatted = String.format(Locale.getDefault(), "%02d:%02d", minutes_down_start, seconds_down_start);
            card_2_down.setText(card_2_down_start_formatted);
            handler.postDelayed(this, 100);
        }
    };

    handler.postDelayed(counter, 100);
}
+4
source share
2 answers

You can use CountDownTimer :

new CountDownTimer(endsIn * 1000, 1000) { 
public void onTick(long millisUntilFinished) { 
           timerTextView.setText(String.valueOf(millisUntilFinished/1000);
 } 
public void onFinish() {
} 
}.start();

OR

expand CountDownTimerclass:

public class countdown_timer extends CountDownTimer {
    TextView textView;
    @Override
    public void onTick(long millisInFuture) {
        long sec = millisInFuture/1000;
            if(millisInFuture <= 0) {
                Log.v("status", "done");

        } else {
            Log.v("status", Long.toString(sec) + " seconds remain and timer has stopped!");
        }
    }

    @Override
    public void onFinish() {
        if(textView != null){
            // change text in your textview
        }
    }



    public countdown_timer(long pMillisInFuture, long pCountDownInterval) {
        super(pMillisInFuture, pCountDownInterval);

    }
    public countdown_timer(TextView textView, long pMillisInFuture, long pCountDownInterval) {
        super(pMillisInFuture, pCountDownInterval);
        this.textView = textView;




    }






}

, , , TextView onFinish().

2:

CountDownTimer :

public class MainActivity extends AppCompatActivity {

    TextView textView;
    CountDownTimer mycounterdown;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        long startcard = 10000;
        textView = (TextView) findViewById(R.id.test);

        mycounterdown = new CountDownTimer(startcard, 1000) {
            @Override
            public void onTick(long mycounterup) {
                int minutes_up_start = (int) (mycounterup / 1000) / 60;
                int seconds_up_start = (int) (mycounterup / 1000) % 60;
                String time_2_up_start_formatted = String.format(Locale.getDefault(), "%02d:%02d", minutes_up_start, seconds_up_start);
                textView.setText(time_2_up_start_formatted);
            }

            @Override
            public void onFinish() {
                // call here other methods from activity

                testMethod();
            }
        };
        mycounterdown.start();

    }
    public void testMethod(){
        Toast.makeText(MainActivity.this, "Test Method called", Toast.LENGTH_SHORT).show();
    }

}

3:, , 500 1000:

public class MainActivity extends AppCompatActivity {

    TextView textView;
    CountDownTimer mycounterdown;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        long startcard = 10000;
        textView = (TextView) findViewById(R.id.test);

        mycounterdown = new CountDownTimer(startcard, 500) {
            @Override
            public void onTick(long mycounterup) {
                int minutes_up_start = (int) (mycounterup / 1000) / 60;
                int seconds_up_start = (int) (mycounterup / 1000) % 60;
                String time_2_up_start_formatted = String.format(Locale.getDefault(), "%02d:%02d", minutes_up_start, seconds_up_start);
                textView.setText(time_2_up_start_formatted);
            }

            @Override
            public void onFinish() {
                // call here other methods from activity

                testMethod();
            }
        };
        mycounterdown.start();

    }
    public void testMethod(){
        Toast.makeText(MainActivity.this, "Test Method called", Toast.LENGTH_SHORT).show();
    }

}

.

+4

CountDownTimer .

public class countdown_timer extends CountDownTimer {

}

.

@Override
public void onTick(long l) {

}

@Override
public void onFinish() {

}

, . . TextView

TextView textView;
public countdown_timer(long millisInFuture, long countDownInterval, TextView txt) {
    super(millisInFuture, countDownInterval);
    textView = txt;
}

onFinish() - , . , CountDownTimer. . , .

+1

Source: https://habr.com/ru/post/1694762/


All Articles