miloJ
miloJ

Reputation: 17

Advice for fixing my countdowntimer in android

public class MainActivity extends Activity {
private CountDownTimer countDownTimer;
private CountDownTimer countDownTimer2;//second timer count down

private boolean timerStart = false;//timer start
private boolean timerStart2 = false;

private Button startButton;//timer button
private Button startButton2;

public TextView text;
public TextView text2;//text field for second timer

public TextView text3;// text 3 and 4 meant to hold the elapsed time for each timer
public TextView text4;

private final long startTime = 90 * 1000;//first timer
private final long interval = 1 * 1000;

private final long startTime2 = 30 * 1000; //second timer time
private final long interval2 = 1 * 1000;

TextToSpeech tts;

private class countDownTimerListener implements OnClickListener {

    @Override
    public void onClick(View v) {
        if (!timerStart){
            countDownTimer.start();
            timerStart = true;
            startButton.setText("Stop");
            startButton2.setEnabled(false);
        } else {
            countDownTimer.cancel();
            timerStart = false;
            startButton.setText("Restart");             
        }                   
    }       
}
private class countDownTimerListener2 implements OnClickListener {

    @Override
    public void onClick(View v) {
        if (!timerStart2){
            countDownTimer2.start();
            timerStart2 = true;
            startButton2.setText("Stop");
            startButton.setEnabled(false);
        } else {
            countDownTimer2.cancel();
            timerStart2 = false;
            startButton2.setText("Restart");
        }                   
    }       
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startButton = (Button)this.findViewById(R.id.button1);
    startButton2 = (Button)this.findViewById(R.id.button2);

    startButton.setOnClickListener(new countDownTimerListener());
    startButton2.setOnClickListener(new countDownTimerListener2());

    text = (TextView) this.findViewById(R.id.textView1);
    text2 = (TextView) this.findViewById(R.id.textView4);//second timer text

    text3 = (TextView) this.findViewById(R.id.textView5);//text field for elapsed time
    text4 = (TextView) this.findViewById(R.id.textView7);
    //this is where I take the text view from xml and assign to text 3 and 4.
    countDownTimer = new MyTimer(startTime, interval, "You have completed your 90 second Timer", text, text3);//first timer
    text.setText(text.getText() + String.valueOf(startTime/1000));

    countDownTimer2 = new MyTimer(startTime2, interval2, "You have completed your 30 second Timer", text2, text4);//second timer start
    text2.setText(text2.getText() + String.valueOf(startTime2/1000));//second timer text field

    tts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {

        @Override
        public void onInit(int status) {
            if(status != TextToSpeech.ERROR){
                tts.setLanguage(Locale.US);
            }
        }                           
    });
}

public class MyTimer extends CountDownTimer { 
    private String ttsString;
    private TextView textview;
    private TextView timeElapsedView;
    private long timeElapsed;
    private long startTime;

    public MyTimer(long startTime, long interval, String ttsString, TextView textview, TextView timeElapsedView){
        super(startTime, interval);
        this.ttsString = ttsString;
        this.textview = textview;
        this.timeElapsedView = timeElapsedView;
    }

    @Override
    public void onTick(long millisUntilFinished) {
        textview.setText("" + (millisUntilFinished / 1000));
        //startTime - millisUntilFinished elapsed time
        timeElapsed = ((startTime - millisUntilFinished) / 1000);
        timeElapsedView.setText("" + String.valueOf(timeElapsed));
    }

    @Override
    public void onFinish() {
        textview.setText("Time's up!"); 
        //Vibrator vibrator = (Vibrator)this.getSystemService(VIBRATOR_SERVICE);
        //vibrator.vibrate(600);
        ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 100);
        toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 200);
        tts.speak(ttsString, TextToSpeech.QUEUE_ADD, null);     
    }
}   

}

I'm am trying to create a countdown timer in Java for android. I want it to show the elapsed time, starting from 1, and the remaining time counting down. I can get the countdown time to work fine but when I do elapsed time, it starts from 89971 and counts up from there. Any advice on how to fix this? This is the code I have that I am using for the timer to appear.

Upvotes: 0

Views: 225

Answers (1)

Mjoellnir
Mjoellnir

Reputation: 486

Where does your startTime within the onTick() method come from? If it's an instance-variable of your MyTimer-class, you have forgotten to mention that. Furthermore, it's a long, a time-value in milliseconds just as the millisInFuture-value. That means, you should do something like this:

timeElapsed = (startTime - millisUntilFinished) / 1000;

And you can make timeElapsed a local variable of the onTick()-method.

Update after disclosure of full code:

Please add a startTime instance variable to your MyTimer-class, which you initialize properly in the MyTimer-constructor, because you use MyTimer in two places with two different start times, but in onTick(), you use the startTime instance variable from the outer class, which is only valid for the first instance (countDownTimer).

Upvotes: 1

Related Questions