The CountDownTimer class enables you to set a time interval with periodic notifications during the countdown.
See: http://developer.android.com/reference/android/os/CountDownTimer.html
Main Steps
1. Set the time duration of the countdown in milliseconds.
Example: 4000 milliseconds.
2. Set the notification interval in milliseconds.
Example: 1000 milliseconds.
3. Put the action you want performed after the countdown in the onFinish() method.
See my bug report here on the CountDownTimer:
https://sites.google.com/site/myrononmobileapps/reference-material/bugs
The bug is the log statement after start(); runs before the countdown instead of after it.
Code Example
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView txt_1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt_1 = (TextView) findViewById(R.id.txt_1);
pause();
}
void pause() {
new CountDownTimer(4000, 1000) {
public void onTick(long millisUntilFinished) {
txt_1.setText("Seconds: " + millisUntilFinished/1000);
Log.d("In CountDownTimer ", "onTick() method.");
}
public void onFinish() {
txt_1.setText("Done!");
Log.d("In CountDownTimer ", "onFinish() method.");
}
}.start();
Log.d("In Pause ", "after New CountDownTimer statement.");
}
}