You can try this:
Note: I'm not using any service in this example, so I've used a postDelayed
which executes the code after 5 seconds. Based on that, I'm calculating the duration from the start of the activity to the end of the postDelayed
long delayedTime, currTime;
TextView text;
currTime = System.currentTimeMillis();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
delayedTime = System.currentTimeMillis();
text.setText("" + getDurationString((int)((delayedTime - currTime) * 0.001)));
}
}, 5000);
--
public String getDurationString(int seconds) {
int hours = seconds / 3600;
int minutes = (seconds % 3600) / 60;
seconds = seconds % 60;
return twoDigitString(hours) + " : " + twoDigitString(minutes) + " : " + twoDigitString(seconds);
}
private String twoDigitString(int number) {
if (number == 0) {
return "00";
}
if (number / 10 == 0) {
return "0" + number;
}
return String.valueOf(number);
}