import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;
import android.content.ContentResolver;
import android.content.Context;
import android.os.Handler;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.widget.DigitalClock;
public class TimeClock extends DigitalClock {
private Calendar calender;
private Runnable runnable;
private Handler handler;
private boolean mt = false;
String timeStr = "";
private ContentResolver cr;
public TimeClock(Context context) {
super(context);
// TODO Auto-generated constructor stub
initClock(context);
}
public TimeClock(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
initClock(context);
}
private void initClock(Context context) {
cr = getContext().getContentResolver();
if (calender == null) {
calender = Calendar.getInstance();
}
}
@Override
protected void onAttachedToWindow() {
// TODO Auto-generated method stub
super.onAttachedToWindow();
mt = false;
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
if (mt)
return;
calender = Calendar.getInstance(TimeZone.getDefault());
calender.setTimeInMillis(System.currentTimeMillis());
String str = android.provider.Settings.System.getString(cr, android.provider.Settings.System.TIME_12_24);
if (str == null || str.equals("")) {
str = "24";
}
str = "24";
if (str.equals("24")) {
timeStr = new SimpleDateFormat("HH:mm").format(calender.getTime());
} else {
timeStr = new SimpleDateFormat("hh:mm").format(calender.getTime());
}
setText(timeStr);
invalidate();
long now = SystemClock.uptimeMillis();
long next = now + (1000 - now % 1000);
handler.postAtTime(runnable, next);
}
};
runnable.run();
}
@Override
protected void onDetachedFromWindow() {
// TODO Auto-generated method stub
super.onDetachedFromWindow();
mt = true;
}
}
import java.util.Calendar;
import java.util.TimeZone;
import android.content.ContentResolver;
import android.content.Context;
import android.os.Handler;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.widget.DigitalClock;
public class TimeClock extends DigitalClock {
private Calendar calender;
private Runnable runnable;
private Handler handler;
private boolean mt = false;
String timeStr = "";
private ContentResolver cr;
public TimeClock(Context context) {
super(context);
// TODO Auto-generated constructor stub
initClock(context);
}
public TimeClock(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
initClock(context);
}
private void initClock(Context context) {
cr = getContext().getContentResolver();
if (calender == null) {
calender = Calendar.getInstance();
}
}
@Override
protected void onAttachedToWindow() {
// TODO Auto-generated method stub
super.onAttachedToWindow();
mt = false;
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
if (mt)
return;
calender = Calendar.getInstance(TimeZone.getDefault());
calender.setTimeInMillis(System.currentTimeMillis());
String str = android.provider.Settings.System.getString(cr, android.provider.Settings.System.TIME_12_24);
if (str == null || str.equals("")) {
str = "24";
}
str = "24";
if (str.equals("24")) {
timeStr = new SimpleDateFormat("HH:mm").format(calender.getTime());
} else {
timeStr = new SimpleDateFormat("hh:mm").format(calender.getTime());
}
setText(timeStr);
invalidate();
long now = SystemClock.uptimeMillis();
long next = now + (1000 - now % 1000);
handler.postAtTime(runnable, next);
}
};
runnable.run();
}
@Override
protected void onDetachedFromWindow() {
// TODO Auto-generated method stub
super.onDetachedFromWindow();
mt = true;
}
}