FileDocCategorySizeDatePackage
Chronometer.javaAPI DocAndroid 1.5 API8728Wed May 06 22:41:56 BST 2009android.widget

Chronometer

public class Chronometer extends TextView
Class that implements a simple timer.

You can give it a start time in the {@link SystemClock#elapsedRealtime} timebase, and it counts up from that, or if you don't give it a base time, it will use the time at which you call {@link #start}. By default it will display the current timer value in the form "MM:SS" or "H:MM:SS", or you can use {@link #setFormat} to format the timer value into an arbitrary string.

attr
ref android.R.styleable#Chronometer_format

Fields Summary
private static final String
TAG
private long
mBase
private boolean
mVisible
private boolean
mStarted
private boolean
mRunning
private boolean
mLogged
private String
mFormat
private Formatter
mFormatter
private Locale
mFormatterLocale
private Object[]
mFormatterArgs
private StringBuilder
mFormatBuilder
private OnChronometerTickListener
mOnChronometerTickListener
private StringBuilder
mRecycle
private static final int
TICK_WHAT
private android.os.Handler
mHandler
Constructors Summary
public Chronometer(android.content.Context context)
Initialize this Chronometer object. Sets the base to the current time.

    
                    
       
        this(context, null, 0);
    
public Chronometer(android.content.Context context, android.util.AttributeSet attrs)
Initialize with standard view layout information. Sets the base to the current time.

        this(context, attrs, 0);
    
public Chronometer(android.content.Context context, android.util.AttributeSet attrs, int defStyle)
Initialize with standard view layout information and style. Sets the base to the current time.

        super(context, attrs, defStyle);

        TypedArray a = context.obtainStyledAttributes(
                attrs,
                com.android.internal.R.styleable.Chronometer, defStyle, 0);
        setFormat(a.getString(com.android.internal.R.styleable.Chronometer_format));
        a.recycle();

        init();
    
Methods Summary
voiddispatchChronometerTick()


      
        if (mOnChronometerTickListener != null) {
            mOnChronometerTickListener.onChronometerTick(this);
        }
    
public longgetBase()
Return the base time as set through {@link #setBase}.

        return mBase;
    
public java.lang.StringgetFormat()
Returns the current format string as set through {@link #setFormat}.

        return mFormat;
    
public android.widget.Chronometer$OnChronometerTickListenergetOnChronometerTickListener()

return
The listener (may be null) that is listening for chronometer change events.

        return mOnChronometerTickListener;
    
private voidinit()

        mBase = SystemClock.elapsedRealtime();
        updateText(mBase);
    
protected voidonDetachedFromWindow()

        super.onDetachedFromWindow();
        mVisible = false;
        updateRunning();
    
protected voidonWindowVisibilityChanged(int visibility)

        super.onWindowVisibilityChanged(visibility);
        mVisible = visibility == VISIBLE;
        updateRunning();
    
public voidsetBase(long base)
Set the time that the count-up timer is in reference to.

param
base Use the {@link SystemClock#elapsedRealtime} time base.

        mBase = base;
        dispatchChronometerTick();
        updateText(SystemClock.elapsedRealtime());
    
public voidsetFormat(java.lang.String format)
Sets the format string used for display. The Chronometer will display this string, with the first "%s" replaced by the current timer value in "MM:SS" or "H:MM:SS" form. If the format string is null, or if you never call setFormat(), the Chronometer will simply display the timer value in "MM:SS" or "H:MM:SS" form.

param
format the format string.

        mFormat = format;
        if (format != null && mFormatBuilder == null) {
            mFormatBuilder = new StringBuilder(format.length() * 2);
        }
    
public voidsetOnChronometerTickListener(android.widget.Chronometer$OnChronometerTickListener listener)
Sets the listener to be called when the chronometer changes.

param
listener The listener.

        mOnChronometerTickListener = listener;
    
public voidsetStarted(boolean started)
The same as calling {@link #start} or {@link #stop}.

hide
pending API council approval

        mStarted = started;
        updateRunning();
    
public voidstart()
Start counting up. This does not affect the base as set from {@link #setBase}, just the view display. Chronometer works by regularly scheduling messages to the handler, even when the Widget is not visible. To make sure resource leaks do not occur, the user should make sure that each start() call has a reciprocal call to {@link #stop}.

        mStarted = true;
        updateRunning();
    
public voidstop()
Stop counting up. This does not affect the base as set from {@link #setBase}, just the view display. This stops the messages to the handler, effectively releasing resources that would be held as the chronometer is running, via {@link #start}.

        mStarted = false;
        updateRunning();
    
private voidupdateRunning()

        boolean running = mVisible && mStarted;
        if (running != mRunning) {
            if (running) {
                updateText(SystemClock.elapsedRealtime());
                dispatchChronometerTick();
                mHandler.sendMessageDelayed(Message.obtain(mHandler, TICK_WHAT), 1000);
            } else {
                mHandler.removeMessages(TICK_WHAT);
            }
            mRunning = running;
        }
    
private synchronized voidupdateText(long now)

        long seconds = now - mBase;
        seconds /= 1000;
        String text = DateUtils.formatElapsedTime(mRecycle, seconds);

        if (mFormat != null) {
            Locale loc = Locale.getDefault();
            if (mFormatter == null || !loc.equals(mFormatterLocale)) {
                mFormatterLocale = loc;
                mFormatter = new Formatter(mFormatBuilder, loc);
            }
            mFormatBuilder.setLength(0);
            mFormatterArgs[0] = text;
            try {
                mFormatter.format(mFormat, mFormatterArgs);
                text = mFormatBuilder.toString();
            } catch (IllegalFormatException ex) {
                if (!mLogged) {
                    Log.w(TAG, "Illegal format string: " + mFormat);
                    mLogged = true;
                }
            }
        }
        setText(text);