FileDocCategorySizeDatePackage
DigitalClock.javaAPI DocAndroid 1.5 API3629Wed May 06 22:41:56 BST 2009android.widget

DigitalClock

public class DigitalClock extends TextView
Like AnalogClock, but digital. Shows seconds. FIXME: implement separate views for hours/minutes/seconds, so proportional fonts don't shake rendering

Fields Summary
Calendar
mCalendar
private static final String
m12
private static final String
m24
private FormatChangeObserver
mFormatChangeObserver
private Runnable
mTicker
private android.os.Handler
mHandler
private boolean
mTickerStopped
String
mFormat
Constructors Summary
public DigitalClock(android.content.Context context)


       
        super(context);
        initClock(context);
    
public DigitalClock(android.content.Context context, android.util.AttributeSet attrs)

        super(context, attrs);
        initClock(context);
    
Methods Summary
private booleanget24HourMode()
Pulls 12/24 mode from system settings

        return android.text.format.DateFormat.is24HourFormat(getContext());
    
private voidinitClock(android.content.Context context)

        Resources r = mContext.getResources();

        if (mCalendar == null) {
            mCalendar = Calendar.getInstance();
        }

        mFormatChangeObserver = new FormatChangeObserver();
        getContext().getContentResolver().registerContentObserver(
                Settings.System.CONTENT_URI, true, mFormatChangeObserver);

        setFormat();
    
protected voidonAttachedToWindow()

        mTickerStopped = false;
        super.onAttachedToWindow();
        mHandler = new Handler();

        /**
         * requests a tick on the next hard-second boundary
         */
        mTicker = new Runnable() {
                public void run() {
                    if (mTickerStopped) return;
                    mCalendar.setTimeInMillis(System.currentTimeMillis());
                    setText(DateFormat.format(mFormat, mCalendar));
                    invalidate();
                    long now = SystemClock.uptimeMillis();
                    long next = now + (1000 - now % 1000);
                    mHandler.postAtTime(mTicker, next);
                }
            };
        mTicker.run();
    
protected voidonDetachedFromWindow()

        super.onDetachedFromWindow();
        mTickerStopped = true;
    
private voidsetFormat()

        if (get24HourMode()) {
            mFormat = m24;
        } else {
            mFormat = m12;
        }