http://www.cnblogs.com/over140/archive/2010/08/27/1809873.html
改了一下,但是输出格式未能实现自定义,原因在于下面的代码中显示时间差不正确,我不知道什么原因。
mFormat = "距离结束还有dd天kk小时mm分ss秒";//yyyy-MM-dd hh:mm:ss
mCalendar.setTimeInMillis(mTimeDistance);//为什么这样计算的时间不对???
setText(DateFormat.format(mFormat, mCalendar));
我只能退一步,将就着了,源码是这样的:
用法:
改了一下,但是输出格式未能实现自定义,原因在于下面的代码中显示时间差不正确,我不知道什么原因。
mFormat = "距离结束还有dd天kk小时mm分ss秒";//yyyy-MM-dd hh:mm:ss
mCalendar.setTimeInMillis(mTimeDistance);//为什么这样计算的时间不对???
setText(DateFormat.format(mFormat, mCalendar));
我只能退一步,将就着了,源码是这样的:
import java.util.Calendar; import android.content.Context; import android.database.ContentObserver; import android.os.Handler; import android.os.SystemClock; import android.provider.Settings; import android.text.format.DateFormat; import android.util.AttributeSet; import android.util.Log; public class CountDownDigitalClock extends android.widget.DigitalClock { Calendar mCalendar; private final static String m12 = "h:mm aa";// h:mm:ss aa private final static String m24 = "k:mm";// k:mm:ss private FormatChangeObserver mFormatChangeObserver; private Runnable mTicker; private Handler mHandler; private boolean mTickerStopped = false; String mFormat; private long mDeadTime; private OnCountDownListener onCountDownListener; public CountDownDigitalClock(Context context) { super(context); initClock(context); } public CountDownDigitalClock(Context context, AttributeSet attrs) { super(context, attrs); initClock(context); } private void initClock(Context context) { if (mCalendar == null) { mCalendar = Calendar.getInstance(); } mFormatChangeObserver = new FormatChangeObserver(); getContext().getContentResolver().registerContentObserver( Settings.System.CONTENT_URI, true, mFormatChangeObserver); setFormat(); } @Override protected void onAttachedToWindow() { 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; long mCurrentTime = System.currentTimeMillis(); if (mCurrentTime >= mDeadTime) { if (onCountDownListener != null){ onCountDownListener.onFinish(); } return; } long mTimeDistance = mDeadTime - mCurrentTime; long between = mTimeDistance / 1000;// 转换成秒 long day = between / (24 * 3600); long hour = between % (24 * 3600) / 3600; long minute = between % (24 * 3600) % 3600 / 60; long second = between % (24 * 3600) % 3600 % 60; String deadTimeStr = "距离结束还有" + day + "天" + hour + "小时" + minute + "分" + second + "秒"; setText(deadTimeStr); // mFormat = "距离结束还有dd天kk小时mm分ss秒";//yyyy-MM-dd hh:mm:ss // mCalendar.setTimeInMillis(mTimeDistance);//为什么这样计算的时间不对??? // setText(DateFormat.format(mFormat, mCalendar)); if (onCountDownListener != null) onCountDownListener.onTick(); invalidate(); long now = SystemClock.uptimeMillis(); long next = now + (1000 - now % 1000); mHandler.postAtTime(mTicker, next); } }; mTicker.run(); } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); mTickerStopped = true; } /** * Pulls 12/24 mode from system settings */ private boolean get24HourMode() { return android.text.format.DateFormat.is24HourFormat(getContext()); } private void setFormat() { if (get24HourMode()) { mFormat = m24; } else { mFormat = m12; } } private class FormatChangeObserver extends ContentObserver { public FormatChangeObserver() { super(new Handler()); } @Override public void onChange(boolean selfChange) { setFormat(); } } /** * set the dead time * * @param deadtime */ public void setDeadTime(long deadTime) { this.mDeadTime = deadTime; } public interface OnCountDownListener { public void onFinish(); public void onTick(); } public void setOnCountDownListener(OnCountDownListener onCountDownListener) { this.onCountDownListener = onCountDownListener; } }
用法:
mClock = (CountDownDigitalClock) findViewById(R.id.myClock); mClock.setDeadTime(getDeadTimeFromServer()); mClock.setOnCountDownListener(new CountDownDigitalClock.OnCountDownListener() { @Override public void onFinish() { // TODO Auto-generated method stub showToast("倒计时结束!!!"); } @Override public void onTick() { // TODO Auto-generated method stub Log.i("tag", "执行了"+(count++)+"次"); } }); private long getDeadTimeFromServer(){ Calendar mCalendar = Calendar.getInstance(); mCalendar.set(2012, 5-1, 18);//月份从0开始 mCalendar.set(Calendar.HOUR_OF_DAY, 13);//下午1点 mCalendar.set(Calendar.MINUTE, 0); mCalendar.set(Calendar.SECOND, 0); return mCalendar.getTimeInMillis(); }