i am working on a android app just like countdown. at this point i am doing some basic functionalities.
Runnable updater;
int b = 0;
void updateTime(final int timeString) // passing milliseconds
{
b = timeString;
timer = (TextView) findViewById(R.id.timerText);
final Handler timerHandler = new Handler();
updater = new Runnable() {
@Override
public void run() {
if (b >= 1000) {
timer.setText("" + b);
//decrease 1 sec
b = b - 1000;
timerHandler.postDelayed(updater, 1000);
} else {
timerHandler.removeCallbacks(updater);
}
}
};
timerHandler.post(updater);
}
in above code i just tried to pass milliseconds as a parameter and then printing the remaining seconds on timer textview. after every seconds.
so its working fine. its updating textview. like suppose i passed 5000 as parameter then output: 5000 4000 3000 and so on..and stops after reaching at 1000
but now the problem is i want to convert those milliseconds to Days:hr:min:sec and then should it print. so, i tried as
Runnable updater;
int b = 0;
void updateTime(final int timeString) {
b = timeString;
timer = (TextView) findViewById(R.id.timerText);
final Handler timerHandler = new Handler();
updater = new Runnable() {
@Override
public void run() {
if (b >= 1000) {
int Final=b;
int day,hr,min,sec;
day = Final / (60 * 60 * 24);
Final -= day * (60 * 60 * 24);
hr = Final / (60 * 60);
Final -= hr * (60 * 60);
min = Final / 60;
sec = Final - min * 60;
timer.setText(day + " Days " + hr + " Hours " + min + " minutes " + sec + " Sec");
//decrease 1 sec
b = b - 1000;
timerHandler.postDelayed(updater, 1000);
} else {
timerHandler.removeCallbacks(updater);
}
}
};
timerHandler.post(updater);
}
but its not giving correct outpout. i have already used this conversion technique before...but its not working in above case... plz someone tell me, where i am making mistake.please
please note: no i dont want to convert millisec into dd/mm/yyyy format. let say i have 5000 millisec i.s 5 sec. so the handler block should first display 00 days 00 hrs 00 min 5 sec. then after every sec it should decrease 1000 from 5000 i.e 4000 so it will display ........ 04 sec and so on.. until......00 sec ...( just like countdown )