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);
这是布局文件:
<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开发学习笔记--计时器的应用实例的更多相关文章
- android开发学习笔记000
使用书籍:<疯狂android讲义>——李刚著,2011年7月出版 虽然现在已2014,可我挑来跳去,还是以这本书开始我的android之旅吧. “疯狂源自梦想,技术成就辉煌.” 让我这个 ...
- 【转】Android开发学习笔记(一)——初识Android
对于一名程序员来说,“自顶向下”虽然是一种最普通不过的分析问题和解决问题的方式,但其却是简单且较为有效的一种.所以,将其应用到Android的学习中来,不至于将自己的冲动演变为一种盲目和不知所措. 根 ...
- 【转】Android开发学习笔记:5大布局方式详解
Android中常用的5大布局方式有以下几种: 线性布局(LinearLayout):按照垂直或者水平方向布局的组件. 帧布局(FrameLayout):组件从屏幕左上方布局组件. 表格布局(Tabl ...
- 【Android开发学习笔记之一】5大布局方式详解
Android中常用的5大布局方式有以下几种: 线性布局(LinearLayout):按照垂直或者水平方向布局的组件. 帧布局(FrameLayout):组件从屏幕左上方布局组件. 表格布局(Tabl ...
- android开发学习笔记系列(1)-android起航
前言 在学习安卓的过程中,我觉得非常有必要将自己所学的东西进行整理,因为每每当我知道我应该是如何去实现功能的时候,有许多细节问题我总是会遗漏,因此我也萌生了写一系列博客来描述自己学习的路线,让我的an ...
- Android开发学习笔记DDMS的使用
打开DDMS DDMS 的全称是Dalvik Debug Monitor Service,是 Android 开发环境中的Dalvik虚拟机调试监控服务. DDMS里面包含了:Device(设备) F ...
- Android开发学习笔记(二)——编译和运行原理(1)
http://www.cnblogs.com/Pickuper/archive/2011/06/14/2078969.html 接着上一篇的内容,继续从全局了解Android.在清楚了Android的 ...
- android开发学习笔记系列(2)-android应用界面编程
前言 本篇博客将会简要介绍andriod开发过程中的一些界面元素和编程的实现,我将大家走进安卓的XML世界,当然可能会涉及到java代码,当然本文主要是介绍XML文件的界面布局. 那么我们的XML存在 ...
- Android开发学习笔记:浅谈GridView
GridView(网格视图)是按照行列的方式来显示内容的,一般用于显示图片,图片等内容,比如实现九宫格图,用GridView是首选,也是最简单的.主要用于设置Adapter. GridView常用的X ...
随机推荐
- js024-最佳实践
js024-最佳实践 本章内容: 可维护的代码 保证代码性能 部署代码 24.1 可维护性 24.1.1 代码的可维护性 代码可维护性的特征: 特性 说明 可理解性 其他人可以理解它的用途和一般途径 ...
- asp.net js 倒计时总秒数量 和 排序
Edit in JSFiddle JavaScript HTML CSS Result h1 { font-family: "微软雅黑"; font-size: 40px; mar ...
- CURL函数的GET和POST方式的两种写法(实现ajax跨域调用)
POST请求 function curl_post($url='',$postdata='',$options=array()){ $ch=curl_init($url); curl_setopt($ ...
- VC----SDK下对窗口非客户区的操作
窗口分成两大部分:客户区和非客户区.非客户区再次细分:标题栏,如图片中顶部深蓝色:左边框,如图片中红色部分:上边框,如图片中绿色部分:右边框,如图片中右侧天蓝色部分:底边框,如图片中下面棕色部分. 之 ...
- Xcode设置
1. 在Search Paths中设置相对路径 径是以.xcodeproj文件为基准,./表示与.xcodeproj同级,../表示上一级 2. 链接动态链接库 设置链接库,在Build Settin ...
- (转)JS Date格式化为yyyy-MM-dd类字符串
Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1, //month &quo ...
- C-基本语法与运算
编译: Technorati 标记: C 1, 编译compilers 命令make 将高级语言转换为低级语言. clang: 1,预处理(preprocessing) 2,编译(complition ...
- Yii2 rules验证规则
Rules验证规则: required : 必须值验证属性||CRequiredValidator 的别名, 确保了特性不为空. [['字段名1','字段名2'],required] //字段 ...
- php-fpm进程关闭与重启脚本详解(转)
先来理解一下什么是php-fpm PHP-FPM是一个PHP FastCGI管理器,是只用于PHP的. PHP-FPM其实是PHP源代码的一个补丁,旨在将FastCGI进程管理整合进PHP包中.必须将 ...
- cad中关于点样式点的绘制
点样式 从0开始, 默认的就是0 0= 一个小点; 1= 空的, 什么都不显示; 2= +加号; 3= X 叉号 设置点样式的命令是: pdmode: 可以假设认为是: point default m ...