首页 > 其他分享 >android计时器类CountDownTime的运…

android计时器类CountDownTime的运…

时间:2023-04-30 19:09:58浏览次数:49  
标签:mRemainTime millisInFuture removeMessages mHandler 计时器 MSG android CountDownTime


 

android计时器类CountDownTime的运…_android


1. importandroid.os.Handler;  
2. importandroid.os.Message;  
3.  
4. publicabstractclassAdvancedCountdownTimer {  
5.  
6.    privatefinallongmCountdownInterval;  
7.  
8.    privatelongmTotalTime;  
9.  
10.    privatelongmRemainTime;  
11.  
12.      
13.    publicAdvancedCountdownTimer(longmillisInFuture, longcountDownInterval) {  
14.        mTotalTime = millisInFuture;  
15.        mCountdownInterval = countDownInterval;  
16.  
17.        mRemainTime = millisInFuture;  
18.    }  
19.  
20.    publicfinalvoidseek(intvalue) {  
21.        synchronized(AdvancedCountdownTimer.this) {  
22.            mRemainTime = ((100- value) * mTotalTime) / 100;  
23.        }  
24.    }  
25.  
26.      
27.    publicfinalvoidcancel() {  
28.        mHandler.removeMessages(MSG_RUN);  
29.        mHandler.removeMessages(MSG_PAUSE);  
30.    }  
31.  
32.    publicfinalvoidresume() {  
33.        mHandler.removeMessages(MSG_PAUSE);  
34.        mHandler.sendMessageAtFrontOfQueue(mHandler.obtainMessage(MSG_RUN));  
35.    }  
36.  
37.    publicfinalvoidpause() {  
38.        mHandler.removeMessages(MSG_RUN);  
39.        mHandler.sendMessageAtFrontOfQueue(mHandler.obtainMessage(MSG_PAUSE));  
40.    }  
41.  
42.      
43.    publicsynchronizedfinalAdvancedCountdownTimer start() {  
44.        if(mRemainTime <= 0) {  
45.            onFinish();  
46.            returnthis;  
47.        }  
48.        mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_RUN),  
49.                mCountdownInterval);  
50.        returnthis;  
51.    }  
52.  
53.    publicabstractvoidonTick(longmillisUntilFinished, intpercent);  
54.  
55.      
56.    publicabstractvoidonFinish();  
57.  
58.    privatestaticfinalintMSG_RUN = 1;  
59.    privatestaticfinalintMSG_PAUSE = 2;  
60.  
61.    privateHandler mHandler = newHandler() {  
62.  
63.        @Override 
64.        publicvoidhandleMessage(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.                    } elseif(mRemainTime < mCountdownInterval) {  
73.                        sendMessageDelayed(obtainMessage(MSG_RUN), mRemainTime);  
74.                    } else{  
75.  
76.                        onTick(mRemainTime, newLong(100 
77.                                * (mTotalTime - mRemainTime) / mTotalTime)  
78.                                .intValue());  
79.  
80.                      
81.                        sendMessageDelayed(obtainMessage(MSG_RUN),  
82.                                mCountdownInterval);  
83.                    }  
84.                } elseif(msg.what == MSG_PAUSE) {  
85.  
86.                }  
87.            }  
88.        }  
89.    };  
90. }


android自带的CountdownTimer源码: 


 

android计时器类CountDownTime的运…_android


1.  
2.  
3. packageandroid.os;  
4.  
5. importandroid.util.Log;  
6.  
7.  
8. publicabstractclassCountDownTimer {  
9.  
10.     
11.    privatefinallongmMillisInFuture;  
12.  
13.     
14.    privatefinallongmCountdownInterval;  
15.  
16.    privatelongmStopTimeInFuture;  
17.  
18.     
19.    publicCountDownTimer(longmillisInFuture, longcountDownInterval) {  
20.        mMillisInFuture = millisInFuture;  
21.        mCountdownInterval = countDownInterval;  
22.    }  
23.  
24.     
25.    publicfinalvoidcancel() {  
26.        mHandler.removeMessages(MSG);  
27.    }  
28.  
29.     
30.    publicsynchronizedfinalCountDownTimer start() {  
31.        if(mMillisInFuture <= 0) {  
32.            onFinish();  
33.            returnthis;  
34.        }  
35.        mStopTimeInFuture = SystemClock.elapsedRealtime() + mMillisInFuture;  
36.        mHandler.sendMessage(mHandler.obtainMessage(MSG));  
37.        returnthis;  
38.    }  
39.  
40.  
41.     
42.    publicabstractvoidonTick(longmillisUntilFinished);  
43.  
44.     
45.    publicabstractvoidonFinish();  
46.  
47.  
48.    privatestaticfinalintMSG = 1;  
49.  
50.  
51.    // handles counting down 
52.    privateHandler mHandler = newHandler() {  
53.  
54.        @Override 
55.        publicvoidhandleMessage(Message msg) {  
56.  
57.            synchronized(CountDownTimer.this) {  
58.                finallongmillisLeft = mStopTimeInFuture - SystemClock.elapsedRealtime();  
59.  
60.                if(millisLeft <= 0) {  
61.                    onFinish();  
62.                } elseif(millisLeft < mCountdownInterval) {  
63.                    // no tick, just delay until done 
64.                    sendMessageDelayed(obtainMessage(MSG), millisLeft);  
65.                } else{  
66.                    longlastTickStart = SystemClock.elapsedRealtime();  
67.                    onTick(millisLeft);  
68.  
69.                    // take into account user's onTick taking time to execute 
70.                    longdelay = 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 < 0) delay += mCountdownInterval;  
75.  
76.                    sendMessageDelayed(obtainMessage(MSG), delay);  
77.                }  
78.            }  
79.        }  
80.    };  
81. }


这个是CountDownTimer的实际运用例子: 


http://gundumw100.iteye.com/admin/blogs/1057989

标签:mRemainTime,millisInFuture,removeMessages,mHandler,计时器,MSG,android,CountDownTime
From: https://blog.51cto.com/u_548275/6237981

相关文章

  • Android获取其他包的Context实例,…
    Android中有Context的概念,想必大家都知道。Context可以做很多事情,打开activity、发送广播、打开本包下文件夹和数据库、获取classLoader、获取资源等等。如果我们得到了一个包的Context对象,那我们基本上可以做这个包自己能做的大部分事情。         那我们能得到吗?很高......
  • Android播放GIF动画
    "quality="high"type="application/x-shockwave-flash"pluginspage="http://www.macromedia.com/go/getflashplayer">1.<ImageViewandroid:id="@+id/gifpicture"2.android:layout_width="fill_parent&quo......
  • Android 悬浮Activity并可拖动(访…
    天天动听,这款Android手机上的音乐播放器,相信不少朋友都曾用过。不知大家是否注意到,天天动听有一个迷你歌词的特效。 什么效果呢?就是不管你切到什么画面,歌词永远显示,并且可以拖动。类型QQ音乐,在电脑上播放时显示的歌词效果。 下面先来看一下效果。  这个歌词是在......
  • Android中Context详解 ---- 你所不…
    大家好,             Interfacetoglobalinformationaboutanapplicationenvironment.ThisisanabstractclasswhoseimplementationisprovidedbytheAndroidsystem.Itallowsaccesstoapplication-specificresourcesandclasses,aswellasup-c......
  • Android下Excel的操作
    在Android下操作Excel和在j2se下操作是一样的,下载一个jxl.jar包,导入到android工程的构建路径中。加入权限<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permissionandroid:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>......
  • Android下结束进程的方法
    一、结束一个活动Activity要主动的结束一个活动Activity,使用finish方法,而且这个方法最后会调用Activity的生命周期函数onDestroy方法,结束当前的Activity,从任务栈中弹出当前的Activity,激活下一个Activity。当然其他的finish系列方法,我们不在这里做详细讨论,有兴趣的同学可以自......
  • Android 图片操作
    1.packagecom.lp.imageUI;2.3.importandroid.graphics.Bitmap;4.importandroid.graphics.Canvas;5.importandroid.graphics.LinearGradient;6.importandroid.graphics.Matrix;7.importandroid.graphics.Paint;8.importandroid.gr......
  • android之间传递多个对象  IPC也是…
    第一步:新建一个Android工程命名为ObjectTranDemo(类比较多哦!)目录结构如下图: 第二步:修改main.xml布局文件(这里我增加了两个按钮)代码如下<?xmlversion="1.0"encoding="utf-8"?>  <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"an......
  • Android HAL 是如何被调用的
    Android对硬件的调用,google推荐使用HAL的方式进行调用,对于AndriodHAL的写法,可以参考android源码里的hardware目录下几个模块的模版。在看HAL的编写方法的过程中,会发现整个模块貌似没有一个入口。一般说来模块都要有个入口,比如应用程序有main函数,可以为加载器进行加......
  • Android主流屏幕分辨率介绍
    对于Android游戏开发我们不得不像iPhone那样思考兼容Android平板电脑,苹果要考虑iPad、iPhone3GS和iPhone4等屏幕之间的兼容性,对于开发Android游戏而言也不例外,考虑的机型更多。常规的我们可能只考虑QVGA,HVGA,WVGA,FWVGA和DVGA,但是抛去了手机不谈,可能平板使用类似WSVGA的1024......