1

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);
        }
    }
}
  • Not getting the funda that what you want to achieve? – Biraj Zalavadia Aug 09 '14 at 12:00
  • Ok, my goal is to make the timer start again automatically when it ends. And this process should repeat 4 times. –  Aug 09 '14 at 12:12
  • I think you want to play clip 4 times repetedly – Biraj Zalavadia Aug 09 '14 at 12:21
  • Clip ? Maybe you´re confused because of the mediaplayer. The mediaplayer only Plays an Audio while the app is running. It just an added Feature which isn´t that important. –  Aug 09 '14 at 12:38
  • why you have take two timers ? – Biraj Zalavadia Aug 09 '14 at 12:52
  • I use a second timer, because this is an Intervall countdown timer. I want to use it for Sport activities. The 30 seconds is the workout time and the 15 seconds is the rest time. One whole workout contains 4 rounds. So 4 rounds of working out (4 x 30 sec.) and 4 rounds of resting (4x15 sec.). The 15 seconds start automatically after the 30 seconds. –  Aug 09 '14 at 12:57

1 Answers1

1

Try this simple way not need of for loop

public class MainActivity extends Activity {


    CountDownTimer workoutTimer;
    CountDownTimer restTimer;

    @Override
    public void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startWorkoutTimer(0);

            }
        });
        btnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (workoutTimer!=null ){
                    workoutTimer.cancel();
                }
                if (restTimer!=null ){
                    restTimer.cancel();
                }
            }
        });

    }


    public void startWorkoutTimer(final int count ){


        workoutTimer=new CountDownTimer(30000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {

            }

            @Override
            public void onFinish() {

                if(count!=3){
                    startRestTimer(count);
                }

            }
        };
        workoutTimer.start();

    }


    public void startRestTimer(final int count ){


        restTimer=new CountDownTimer(15000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {

            }

            @Override
            public void onFinish() {

                if(count!=3){
                    startWorkoutTimer(count+1);
                }

            }
        };
        restTimer.start();

    }




}
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
  • Hi, I tried to add it to my code, it doesn´t work, because I do a mistake. I send you my full code. It would be great if you could help me. –  Aug 09 '14 at 12:47
  • I tried your edited code, but I when I try to start the app on my phone it crashes: This is my Logcat : 08-10 09:54:31.104 14248-14248/com.stacktesttimer.app E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.stacktesttimer.app, PID: 14248 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stacktesttimer.app/com.stacktesttimer.app.MainActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity... –  Aug 10 '14 at 07:57
  • I don´t know why the Output is always NullPointerExeption. –  Aug 10 '14 at 10:50
  • Sorry, I fixed that. The app starts, but the timer doesn´t. Something must be still missing. –  Aug 10 '14 at 11:48