Android开发学习笔记--计时器的应用实例

为了了解安卓计时器的用法,写了一个秒表的应用,正是这个秒表,让我对Android应用的速度大跌眼镜,我设置了一个计时器,10ms更新一次显示的时间,然后更标准的时间一比较发现,跑10s就有一秒的时间误差,我也是醉了,这也太慢了吧,我想这也是java不适合用来写速度要求高的程序的原因吧。最后我做了一个修改,100ms更新一次,因为这样在1秒钟之内要做的事更少了,这样一来误差便小了很多,但还是有误差,也是很大的,跑几分钟就看出来了,没办法。主要是给出计时器的用法吧。

这里计时器必须要结合Handle句柄使用,否则计时器不能对UI的布局做改变。首先定义一个Handle:

private Handler uiHandle = new Handler(){
public void handleMessage(android.os.Message msg) {
switch(msg.what){
case 1:
if(isRun)
{

/////////这里是你要做的事
}
uiHandle.sendEmptyMessageDelayed(1, 100);
break;
default: break;
}
}
};

上面的那个参数100表示时间间隔是100ms。这段代码放在那个类里面就可以了,不要放在函数里面,作为那个MainActivity类的成员变量。

然后要启动这个计时器只要这样就可以了:uiHandle.sendEmptyMessageDelayed(1, 100);

这是布局文件:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:id="@+id/LinearLayout1"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical" >
 7     <LinearLayout 
 8         android:orientation="horizontal"
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:layout_gravity="center_horizontal">
12         <TextView 
13             android:id="@+id/num1"
14             android:layout_width="wrap_content"
15             android:layout_height="wrap_content"
16             android:hint="00:"
17             android:textSize="50sp"/>
18         <TextView 
19             android:id="@+id/num2"
20             android:layout_width="wrap_content" 
21             android:layout_height="wrap_content"
22             android:textSize="50sp"
23             android:hint="00."
24             />
25         <TextView 
26             android:id="@+id/num3"
27             android:layout_width="wrap_content" 
28             android:layout_height="wrap_content"
29             android:textSize="50sp"
30             android:hint="0"
31             />
32     </LinearLayout>
33     <LinearLayout 
34         android:orientation="horizontal"
35         android:layout_width="wrap_content"
36         android:layout_height="wrap_content"
37         android:layout_gravity="center_horizontal"
38         >
39         <Button 
40             android:id="@+id/start"
41             android:layout_width="wrap_content"
42             android:layout_height="wrap_content"
43             android:onClick="button_start"
44             android:text="@string/start"/>
45         <Button 
46             android:id="@+id/stop"
47             android:onClick="button_stop"
48             android:layout_width="wrap_content"
49             android:layout_height="wrap_content"
50             android:text="@string/stop"/>
51     </LinearLayout>
52 </LinearLayout>
View Code

这是代码:

  1 package com.example.stopwatch;
  2 
  3 import android.os.Bundle;
  4 import android.app.Activity;
  5 import android.view.Menu;
  6 import android.app.Activity;
  7 import android.os.Bundle;
  8 import android.os.Handler;
  9 import android.view.Menu;
 10 import android.view.View;
 11 import android.view.View.OnClickListener;
 12 import android.widget.Button;
 13 import android.widget.TextView;
 14 
 15 public class MainActivity extends Activity {
 16     private TextView num1;        //
 17     private TextView num2;        //
 18     private TextView num3;
 19     private Button start;            //开始按钮
 20     private Button stop;            //停止按钮
 21     private boolean isRun = false;
 22     private int n1,n2,n3;
 23 
 24     private String A="",B="",C="";
 25     
 26     private Handler uiHandle = new Handler(){
 27         public void handleMessage(android.os.Message msg) {
 28             switch(msg.what){
 29             case 1:
 30                 if(isRun) 
 31                 {
 32                     n3 = n3 + 1;    ///更新三个数字
 33                     n2 = n2 + n3 / 10;
 34                     n3 %= 10;
 35                     n1 = n1 + n2 / 60;
 36                     n2 %= 60;
 37                     updateClockUI();
 38                 }
 39                 uiHandle.sendEmptyMessageDelayed(1, 100);
 40                 break;
 41             default: break;
 42             }
 43         }
 44     };
 45     
 46 
 47     @Override
 48     public boolean onCreateOptionsMenu(Menu menu) {
 49         getMenuInflater().inflate(R.menu.main, menu);
 50         return true;
 51     }
 52     @Override
 53     public void onCreate(Bundle savedInstanceState) {
 54         super.onCreate(savedInstanceState);
 55         setContentView(R.layout.activity_main);
 56         
 57         //获取界面的控件
 58         num1 = (TextView) findViewById(R.id.num1);
 59         num2 = (TextView) findViewById(R.id.num2);
 60         num3 = (TextView) findViewById(R.id.num3);
 61         start = (Button) findViewById(R.id.start);
 62         stop = (Button) findViewById(R.id.stop);
 63     }
 64     public void button_start(View v)
 65     {
 66         if(!isRun)
 67         {
 68             uiHandle.removeMessages(1);
 69             uiHandle.sendEmptyMessageDelayed(1, 100);
 70             isRun = true;
 71             start.setText("暂停");
 72         }
 73         else
 74         {
 75             uiHandle.removeMessages(1);
 76             isRun = false;
 77             start.setText("开始");
 78         }
 79     }
 80     public void button_stop(View v)
 81     {
 82         n1 = n2 = n3 = 0;
 83         A = B = C = "";
 84         num1.setText("00:");
 85         num2.setText("00.");
 86         num3.setText("0");
 87         
 88         //timeUsedInsec = 0;
 89     }
 90     
 91     
 92 /*    @Override
 93     protected void onPause() {
 94         // TODO Auto-generated method stub
 95         super.onPause();
 96         isRun = true;
 97     }
 98     
 99     */
100  /*   @Override
101     protected void onResume() {
102         // TODO Auto-generated method stub
103         super.onResume();
104         isPaused = false;
105     }
106     */
107     /**
108      * 更新时间的显示
109      */
110     private void updateClockUI(){
111         A = B = C = "";
112         if(n1 < 10) A = "0";
113         if(n2 < 10) B = "0";
114         A = A + n1+":";
115         B = B + n2+".";
116         C = C + n3;
117         num1.setText(A);
118         num2.setText(B);
119         num3.setText(C);
120     }
121     
122 }
View Code

这是字串文件:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <resources>
 3 
 4     <string name="app_name">Stopwatch</string>
 5     <string name="action_settings">Settings</string>
 6     <string name="hello_world">Hello world!</string>
 7      <string name="start">开始</string>
 8       <string name="stop">复位</string>
 9 
10 </resources>
View Code

 

posted @ 2014-09-18 23:28  xiaxiaosheng  阅读(317)  评论(0编辑  收藏  举报