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 defStyleAttr)Initialize with standard view layout information and style.
Sets the base to the current time.
this(context, attrs, defStyleAttr, 0);
|
public Chronometer(android.content.Context context, android.util.AttributeSet attrs, int defStyleAttr, int defStyleRes)
super(context, attrs, defStyleAttr, defStyleRes);
final TypedArray a = context.obtainStyledAttributes(
attrs, com.android.internal.R.styleable.Chronometer, defStyleAttr, defStyleRes);
setFormat(a.getString(com.android.internal.R.styleable.Chronometer_format));
a.recycle();
init();
|
Methods Summary |
---|
void | dispatchChronometerTick()
if (mOnChronometerTickListener != null) {
mOnChronometerTickListener.onChronometerTick(this);
}
|
public long | getBase()Return the base time as set through {@link #setBase}.
return mBase;
|
public java.lang.String | getFormat()Returns the current format string as set through {@link #setFormat}.
return mFormat;
|
public android.widget.Chronometer$OnChronometerTickListener | getOnChronometerTickListener()
return mOnChronometerTickListener;
|
private void | init()
mBase = SystemClock.elapsedRealtime();
updateText(mBase);
|
protected void | onDetachedFromWindow()
super.onDetachedFromWindow();
mVisible = false;
updateRunning();
|
public void | onInitializeAccessibilityEvent(android.view.accessibility.AccessibilityEvent event)
super.onInitializeAccessibilityEvent(event);
event.setClassName(Chronometer.class.getName());
|
public void | onInitializeAccessibilityNodeInfo(android.view.accessibility.AccessibilityNodeInfo info)
super.onInitializeAccessibilityNodeInfo(info);
info.setClassName(Chronometer.class.getName());
|
protected void | onWindowVisibilityChanged(int visibility)
super.onWindowVisibilityChanged(visibility);
mVisible = visibility == VISIBLE;
updateRunning();
|
public void | setBase(long base)Set the time that the count-up timer is in reference to.
mBase = base;
dispatchChronometerTick();
updateText(SystemClock.elapsedRealtime());
|
public void | setFormat(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.
mFormat = format;
if (format != null && mFormatBuilder == null) {
mFormatBuilder = new StringBuilder(format.length() * 2);
}
|
public void | setOnChronometerTickListener(android.widget.Chronometer$OnChronometerTickListener listener)Sets the listener to be called when the chronometer changes.
mOnChronometerTickListener = listener;
|
public void | setStarted(boolean started)The same as calling {@link #start} or {@link #stop}.
mStarted = started;
updateRunning();
|
public void | start()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 void | stop()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 void | updateRunning()
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 void | updateText(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);
|