Hello, Great forum.....But I have a problem with an App I'm currently working on and having a hard time finding the correct way of doing what I need, Its a simple countdown time that beginner at 2 Min and counts down to 0 then displays a message.............The problem is formatting the output time correctly as in 00:00:00 The following code is what I have found and use.
You will see that in the OnTick I have two methods the second being commented out both methods seam to work however the first show the output as 120 and counts down to 0 then shows the message "And Halt!" I need the output to read correctly as 00:02:00 and then countdown to 00:00:00.
The second one that's commented out shows the correct output as 00:00:00 however does not update however when the time is up it does show the message "And Halt"
And help would be great this is kick my Ass...........I could do the same in C++ without thinking about it, but I'm very new to Java...................So be gentle, Thanks in advance
You will see that in the OnTick I have two methods the second being commented out both methods seam to work however the first show the output as 120 and counts down to 0 then shows the message "And Halt!" I need the output to read correctly as 00:02:00 and then countdown to 00:00:00.
The second one that's commented out shows the correct output as 00:00:00 however does not update however when the time is up it does show the message "And Halt"
And help would be great this is kick my Ass...........I could do the same in C++ without thinking about it, but I'm very new to Java...................So be gentle, Thanks in advance
Code:
package com.droidnova.android;
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.TextView;
public class pu2min extends Activity {
static TextView timeDisplay;
MyCount counter;
static long timeRemaining = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pu2min);
timeDisplay = (TextView) findViewById(R.id.timer);
counter = new MyCount(120000, 1000);
}
public void start(View view) {
counter.start();
}
public static String formatTime(long millis) {
String output = "";
long seconds = millis / 1000;
long minutes = seconds / 60;
long hours = minutes / 60;
long days = hours / 24;
seconds = seconds % 60;
minutes = minutes % 60;
hours = hours % 24;
String secondsD = String.valueOf(seconds);
String minutesD = String.valueOf(minutes);
String hoursD = String.valueOf(hours);
if (seconds < 10)
secondsD = "0" + seconds;
if (minutes < 10)
minutesD = "0" + minutes;
if (hours < 10){
hoursD = "0" + hours;
}
if( days > 0 ){
output = days +"d ";
}
output += hoursD + ":" + minutesD + ":" + secondsD;
return output;
}
public static class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
public void onFinish() {
timeDisplay.setText("And Halt!");
}
public void onTick(long millisUntilFinished) {
timeDisplay.setText("Time Remaining: " + millisUntilFinished / 1000);
//timeDisplay.setText("Time Remaining: " + formatTime(timeRemaining));
}
}
}