CountDownDigitalClock:倒计时的TextView

系统 1552 0
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));


CountDownDigitalClock:倒计时的TextView

我只能退一步,将就着了,源码是这样的:
    
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();
}

  

CountDownDigitalClock:倒计时的TextView


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论