I would like to start repeating two lines of code every 5 seconds when I press the button START and end it, when I press the button STOP. I was trynig with a TimerTask and Handles, but couldn't figure it out how.
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//final int i;
final TextView textView = (TextView) findViewById(R.id.textView);
final Button START_STOP = (Button) findViewById(R.id.START_STOP);
final ImageView random_note = (ImageView) findViewById(R.id.random_note);
final int min = 0;
final int max = 2;
final Integer[] image = { R.drawable.a0, R.drawable.a1,R.drawable.a2 };
START_STOP.setTag(1);
START_STOP.setText("START");
START_STOP.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int status = (Integer) v.getTag();
if (status ==1) {
textView.setText("Hello");
START_STOP.setText("STOP");
v.setTag(0);
final Random random = new Random();
//************************************************************
// I would like to loop next 2 lines of code every 5 seconds.//
int i = random.nextInt(2 - 0 + 1) + 0;
random_note.setImageResource(image[i]);
//************************************************************
}
else
{
textView.setText("Bye");
START_STOP.setText("Let's PLAY!");
v.setTag(1);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
I don't have much more to add, other than to mention the differences between using Handler, CountDownTimer, and regular Timer. As britzl mentioned, the CountDownTimer uses a Handler internally, so that is equivalent to using the handler directly. A handler is used for running Ui stuff, for very short periods of time. An example would be setText for a text view. For computationally intensive tasks, handlers may cause a lag. A timer also can only run short tasks, but it is not necessarily only for UI stuff. For more complicated tasks, a new Thread should be used.
You can use
CountDownTimer
as the following method:And then to call the timer again:
To cancel the timer, you can call
timer.cancel();
Hope it helps!
Using a CountDownTimer as in one of the other answers is one way to do it. Another would be to use a Handler and the postDelayed method:
Here's an example using a Timer and a TimerTask:
You can use RxJava2/RxAndroid2 and create an Observable that emits a message every second (or whatever you want), example with pseudo code:
When you want to stop it, you can simply call:
I find this code much more readable than the other options.