为了了解安卓计时器的用法,写了一个秒表的应用,正是这个秒表,让我对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);

这是布局文件:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<TextView
android:id="@+id/num1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="00:"
android:textSize="50sp"/>
<TextView
android:id="@+id/num2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp"
android:hint="00."
/>
<TextView
android:id="@+id/num3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp"
android:hint="0"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
>
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="button_start"
android:text="@string/start"/>
<Button
android:id="@+id/stop"
android:onClick="button_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/stop"/>
</LinearLayout>
</LinearLayout>

这是代码:

 package com.example.stopwatch;

 import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView; public class MainActivity extends Activity {
private TextView num1; //分
private TextView num2; //秒
private TextView num3;
private Button start; //开始按钮
private Button stop; //停止按钮
private boolean isRun = false;
private int n1,n2,n3; private String A="",B="",C=""; private Handler uiHandle = new Handler(){
public void handleMessage(android.os.Message msg) {
switch(msg.what){
case 1:
if(isRun)
{
n3 = n3 + 1; ///更新三个数字
n2 = n2 + n3 / 10;
n3 %= 10;
n1 = n1 + n2 / 60;
n2 %= 60;
updateClockUI();
}
uiHandle.sendEmptyMessageDelayed(1, 100);
break;
default: break;
}
}
}; @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //获取界面的控件
num1 = (TextView) findViewById(R.id.num1);
num2 = (TextView) findViewById(R.id.num2);
num3 = (TextView) findViewById(R.id.num3);
start = (Button) findViewById(R.id.start);
stop = (Button) findViewById(R.id.stop);
}
public void button_start(View v)
{
if(!isRun)
{
uiHandle.removeMessages(1);
uiHandle.sendEmptyMessageDelayed(1, 100);
isRun = true;
start.setText("暂停");
}
else
{
uiHandle.removeMessages(1);
isRun = false;
start.setText("开始");
}
}
public void button_stop(View v)
{
n1 = n2 = n3 = 0;
A = B = C = "";
num1.setText("00:");
num2.setText("00.");
num3.setText("0"); //timeUsedInsec = 0;
} /* @Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
isRun = true;
} */
/* @Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
isPaused = false;
}
*/
/**
* 更新时间的显示
*/
private void updateClockUI(){
A = B = C = "";
if(n1 < 10) A = "0";
if(n2 < 10) B = "0";
A = A + n1+":";
B = B + n2+".";
C = C + n3;
num1.setText(A);
num2.setText(B);
num3.setText(C);
} }

这是字串文件:

 <?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">Stopwatch</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="start">开始</string>
<string name="stop">复位</string> </resources>

Android开发学习笔记--计时器的应用实例的更多相关文章

  1. android开发学习笔记000

    使用书籍:<疯狂android讲义>——李刚著,2011年7月出版 虽然现在已2014,可我挑来跳去,还是以这本书开始我的android之旅吧. “疯狂源自梦想,技术成就辉煌.” 让我这个 ...

  2. 【转】Android开发学习笔记(一)——初识Android

    对于一名程序员来说,“自顶向下”虽然是一种最普通不过的分析问题和解决问题的方式,但其却是简单且较为有效的一种.所以,将其应用到Android的学习中来,不至于将自己的冲动演变为一种盲目和不知所措. 根 ...

  3. 【转】Android开发学习笔记:5大布局方式详解

    Android中常用的5大布局方式有以下几种: 线性布局(LinearLayout):按照垂直或者水平方向布局的组件. 帧布局(FrameLayout):组件从屏幕左上方布局组件. 表格布局(Tabl ...

  4. 【Android开发学习笔记之一】5大布局方式详解

    Android中常用的5大布局方式有以下几种: 线性布局(LinearLayout):按照垂直或者水平方向布局的组件. 帧布局(FrameLayout):组件从屏幕左上方布局组件. 表格布局(Tabl ...

  5. android开发学习笔记系列(1)-android起航

    前言 在学习安卓的过程中,我觉得非常有必要将自己所学的东西进行整理,因为每每当我知道我应该是如何去实现功能的时候,有许多细节问题我总是会遗漏,因此我也萌生了写一系列博客来描述自己学习的路线,让我的an ...

  6. Android开发学习笔记DDMS的使用

    打开DDMS DDMS 的全称是Dalvik Debug Monitor Service,是 Android 开发环境中的Dalvik虚拟机调试监控服务. DDMS里面包含了:Device(设备) F ...

  7. Android开发学习笔记(二)——编译和运行原理(1)

    http://www.cnblogs.com/Pickuper/archive/2011/06/14/2078969.html 接着上一篇的内容,继续从全局了解Android.在清楚了Android的 ...

  8. android开发学习笔记系列(2)-android应用界面编程

    前言 本篇博客将会简要介绍andriod开发过程中的一些界面元素和编程的实现,我将大家走进安卓的XML世界,当然可能会涉及到java代码,当然本文主要是介绍XML文件的界面布局. 那么我们的XML存在 ...

  9. Android开发学习笔记:浅谈GridView

    GridView(网格视图)是按照行列的方式来显示内容的,一般用于显示图片,图片等内容,比如实现九宫格图,用GridView是首选,也是最简单的.主要用于设置Adapter. GridView常用的X ...

随机推荐

  1. web前端性能优化

    性能优化对于用户体验无疑是非常重要的,下面介绍一些性能优化的方法. 1.减少HTTP请求 http请求越多,那么消耗的时间越多,如果在加上网络很糟糕,那么问题就更多了.且如果网页中的图片.css文件. ...

  2. 利用POI 技术动态替换word模板内容

    项目中需要实现一个功能,动态替换给定模板里面的内容,生成word文档提供下载功能. 中间解决了问题有: 1.页眉的文档logo图片解决,刚开始的时候,HWPFDocument 对象无法读取图片对象(已 ...

  3. 解决Bootstrap模态框切换时页面抖动 or页面滚动条

    Bootstrap为了让所有的页面(这里指内容溢出和不溢出)显示效果一样,采取的方法如下: 当Modal显示时,设置body -- overflow:hidden;margin-right:15px; ...

  4. easyUI文本框textbox笔记

    知识点: 1.图标位置 Icon Align属性,有left和right两个: 2.textbox的setvalue方法,getvalue方法. <div style="margin: ...

  5. Java学习笔记2

    package welcome; public class Constants { public static void main(String[] args){ final double CM_PE ...

  6. MBProgressHUD上传照片进度提示

    第一步,控制器先来个属性 @property (strong, nonatomic) MBProgressHUD *HUD; 第二步,显示与隐藏的调用方法 - (void)hudTipWillShow ...

  7. python 函数基础介绍

    函数是对程序逻辑进行结构化或过程化的一种编程方法.能将整块代码巧妙地隔离成易于管理的小块,把重复代码放在函数中而不是进行大量的拷贝. 一.函数创建 def 函数创建格式如下: def function ...

  8. 【MVC5】画面多按钮提交

    画面上有个多个按钮时,如何绑定到各自的Action上? 1.追加如下MultipleButtonAttribute类 1 using System; 2 using System.Reflection ...

  9. 使用Alcatraz来管理Xcode插件(转)

    转自唐巧的博客:http://blog.devtang.com/blog/2014/03/05/use-alcatraz-to-manage-xcode-plugins/ 简介 Alcatraz是一个 ...

  10. Xunsearch 中文全文搜索

    原文地址:http://www.yiichina.com/code/661 官网地址:http://www.xunsearch.com/ 1.安装 wget http://www.xunsearch. ...