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();
}
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

