Hello I have a timer which I want to restart 4 times in a row always as soon as it ends.
I want to use a for Loop like this: for(int i = 0; i<=3; i++)
My problem is, that I don´t know how to use the loop in my code. I don´t know where to add the i variable.
(I also added a mediaplayer which Plays an Audio at several times. You might notice, that I use 2 cntdowntimers. It´s an Intervalltimer which starts off with 30 secs, automatically Switches to the second timer which starts 15 seconds, after that the programmes should start again like I said erlier)
Here are the methods I used:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStart = (Button)findViewById(R.id.btnStart);
btnStop = (Button)findViewById(R.id.btnStop);
textViewTime = (TextView)findViewById(R.id.textViewTime);
textViewTime.setText("00:00:00");
mp = MediaPlayer.create(this, R.raw.vuvuneu);
btnStart = (Button)this.findViewById(R.id.btnStart);
counter=new CountDownTimer(15000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
...
}
@Override
public void onFinish() {
textViewTime.setText("Done");
mp.start();
}
};
final CounterClass timer = new CounterClass(30000,1000);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timer.start();
mp.start();
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timer.cancel();
}
});
}
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressLint("NewApi")
public class CounterClass extends CountDownTimer {
public CounterClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
counter.start();
mp.start();
}
Thank´s in advance. :)
Full code:
public class MainActivity extends ActionBarActivity {
CountDownTimer counter;
MediaPlayer mp;
Button btnStart, btnStop;
TextView textViewTime;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStart = (Button)findViewById(R.id.btnStart);
btnStop = (Button)findViewById(R.id.btnStop);
textViewTime = (TextView)findViewById(R.id.textViewTime);
textViewTime.setText("00:00:00");
mp = MediaPlayer.create(this, R.raw.vuvuneu);
btnStart = (Button)this.findViewById(R.id.btnStart);
counter=new CountDownTimer(15000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
System.out.println(hms);
textViewTime.setText(hms);
}
@Override
public void onFinish() {
textViewTime.setText("Done");
mp.start();
}
};
final CounterClass timer = new CounterClass(30000,1000);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timer.start();
mp.start();
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timer.cancel();
}
});
}
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressLint("NewApi")
public class CounterClass extends CountDownTimer {
public CounterClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
counter.start();
mp.start();
}
@SuppressLint("NewApi")
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
public void onTick(long millisUntilFinished) {
textViewTime.setText("seconds remaining: " + millisUntilFinished / 1000);
long millis = millisUntilFinished;
String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
System.out.println(hms);
textViewTime.setText(hms);
}
}
}
Try this simple way not need of for loop