android计时器类CountDownTime的运…

最近需要一个倒数计时器,要求实现倒数计时,暂停,继续,和快进快退的功能。Android本身提供了一个CountdownTimer的类,采用Handler的方式实现,但是只提供了倒数计时的功能,对于暂停,继续,快进快退功能未提供支持,于是在CounterDownTimer的基础上重写了一个类,最终满足要求。 
Java代码  收藏代码
  1. import android.os.Handler;  
  2. import android.os.Message;  
  3.   
  4. public abstract class AdvancedCountdownTimer  
  5.   
  6.     private final long mCountdownInterval;  
  7.   
  8.     private long mTotalTime;  
  9.   
  10.     private long mRemainTime;  
  11.   
  12.       
  13.     public AdvancedCountdownTimer(long millisInFuture, long countDownInterval)  
  14.         mTotalTime millisInFuture;  
  15.         mCountdownInterval countDownInterval;  
  16.   
  17.         mRemainTime millisInFuture;  
  18.      
  19.   
  20.     public final void seek(int value)  
  21.         synchronized (AdvancedCountdownTimer.this 
  22.             mRemainTime ((100 value) mTotalTime) 100 
  23.          
  24.      
  25.   
  26.       
  27.     public final void cancel()  
  28.         mHandler.removeMessages(MSG_RUN);  
  29.         mHandler.removeMessages(MSG_PAUSE);  
  30.      
  31.   
  32.     public final void resume()  
  33.         mHandler.removeMessages(MSG_PAUSE);  
  34.         mHandler.sendMessageAtFrontOfQueue(mHandler.obtainMessage(MSG_RUN));  
  35.      
  36.   
  37.     public final void pause()  
  38.         mHandler.removeMessages(MSG_RUN);  
  39.         mHandler.sendMessageAtFrontOfQueue(mHandler.obtainMessage(MSG_PAUSE));  
  40.      
  41.   
  42.       
  43.     public synchronized final AdvancedCountdownTimer start()  
  44.         if (mRemainTime <= 0 
  45.             onFinish();  
  46.             return this 
  47.          
  48.         mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_RUN),  
  49.                 mCountdownInterval);  
  50.         return this 
  51.      
  52.   
  53.     public abstract void onTick(long millisUntilFinished, int percent);  
  54.   
  55.       
  56.     public abstract void onFinish();  
  57.   
  58.     private static final int MSG_RUN 1 
  59.     private static final int MSG_PAUSE 2 
  60.   
  61.     private Handler mHandler new Handler()  
  62.   
  63.         @Override  
  64.         public void handleMessage(Message msg)  
  65.   
  66.             synchronized (AdvancedCountdownTimer.this 
  67.                 if (msg.what == MSG_RUN)  
  68.                     mRemainTime mRemainTime mCountdownInterval;  
  69.   
  70.                     if (mRemainTime <= 0 
  71.                         onFinish();  
  72.                     else if (mRemainTime mCountdownInterval)  
  73.                         sendMessageDelayed(obtainMessage(MSG_RUN), mRemainTime);  
  74.                     else  
  75.   
  76.                         onTick(mRemainTime, new Long(100  
  77.                                 (mTotalTime mRemainTime) mTotalTime)  
  78.                                 .intValue());  
  79.   
  80.                       
  81.                         sendMessageDelayed(obtainMessage(MSG_RUN),  
  82.                                 mCountdownInterval);  
  83.                      
  84.                 else if (msg.what == MSG_PAUSE)  
  85.   
  86.                  
  87.              
  88.          
  89.     };  
  90.  


android自带的CountdownTimer源码: 
Java代码    收藏代码
  1.   
  2.   
  3. package android.os;  
  4.   
  5. import android.util.Log;  
  6.   
  7.   
  8. public abstract class CountDownTimer  
  9.   
  10.       
  11.     private final long mMillisInFuture;  
  12.   
  13.       
  14.     private final long mCountdownInterval;  
  15.   
  16.     private long mStopTimeInFuture;  
  17.   
  18.       
  19.     public CountDownTimer(long millisInFuture, long countDownInterval)  
  20.         mMillisInFuture millisInFuture;  
  21.         mCountdownInterval countDownInterval;  
  22.      
  23.   
  24.       
  25.     public final void cancel()  
  26.         mHandler.removeMessages(MSG);  
  27.      
  28.   
  29.       
  30.     public synchronized final CountDownTimer start()  
  31.         if (mMillisInFuture <= 0 
  32.             onFinish();  
  33.             return this 
  34.          
  35.         mStopTimeInFuture SystemClock.elapsedRealtime() mMillisInFuture;  
  36.         mHandler.sendMessage(mHandler.obtainMessage(MSG));  
  37.         return this 
  38.      
  39.   
  40.   
  41.       
  42.     public abstract void onTick(long millisUntilFinished);  
  43.   
  44.       
  45.     public abstract void onFinish();  
  46.   
  47.   
  48.     private static final int MSG 1 
  49.   
  50.   
  51.     // handles counting down  
  52.     private Handler mHandler new Handler()  
  53.   
  54.         @Override  
  55.         public void handleMessage(Message msg)  
  56.   
  57.             synchronized (CountDownTimer.this 
  58.                 final long millisLeft mStopTimeInFuture SystemClock.elapsedRealtime();  
  59.   
  60.                 if (millisLeft <= 0 
  61.                     onFinish();  
  62.                 else if (millisLeft mCountdownInterval)  
  63.                     // no tick, just delay until done  
  64.                     sendMessageDelayed(obtainMessage(MSG), millisLeft);  
  65.                 else  
  66.                     long lastTickStart SystemClock.elapsedRealtime();  
  67.                     onTick(millisLeft);  
  68.   
  69.                     // take into account user's onTick taking time to execute  
  70.                     long delay lastTickStart mCountdownInterval SystemClock.elapsedRealtime();  
  71.   
  72.                     // special case: user's onTick took more than interval to  
  73.                     // complete, skip to next interval  
  74.                     while (delay 0delay += mCountdownInterval;  
  75.   
  76.                     sendMessageDelayed(obtainMessage(MSG), delay);  
  77.                  
  78.              
  79.          
  80.     };  
  81.  

这个是CountDownTimer的实际运用例子: 
http://gundumw100.iteye.com/admin/blogs/1057989
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值