have a clock I want to display in a TextView.
I would have thought there was a few nice functions to do this, but I couldn't find anything.
I ended up implementing my own way that uses a Handler. What I'm asking though, is there a better (more efficient) way to display a real time updating clock?
private Runnable mUpdateClockTask = new Runnable() {
public void run() {
setTime();
mClockHandler.postDelayed(this, 1000);
}
};
That's my handler, which runs every second, and then my set time and date function is below
TextView mClockView;
public void setTime() {
Calendar cal = Calendar.getInstance();
int minutes = cal.get(Calendar.MINUTE);
if (DateFormat.is24HourFormat(this)) {
int hours = cal.get(Calendar.HOUR_OF_DAY);
mClockView.setText((hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes));
}
else {
int hours = cal.get(Calendar.HOUR);
mClockView.setText(hours + ":" + (minutes < 10 ? "0" + minutes : minutes) + " " + new DateFormatSymbols().getAmPmStrings()[cal.get(Calendar.AM_PM)]);
}
}
Cheers
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…