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 ...
随机推荐
- 解决centos7中python-pip模块不存在的问题
centos 7中python-pip模块不存在,是因为像centos这类衍生的发行版,源跟新滞后,或者不存在.即使使用yum去search python-pip也找不到软件包. 为了使用安装滞后或源 ...
- BZOJ4522: [Cqoi2016]密钥破解
pollard's rho模板题. 调参调到160ms无能为力了,应该是写法问题,不玩了. #include<bits/stdc++.h> using namespace std; typ ...
- redhat安装VMware tools的方法
如果我们仔细看的话, 就会发现在VMware软件界面的左下角处显示着 "you don't have VMware Tools installed",即我们还没安装VMware T ...
- c#正则表达式2
System.Text.RegularExpressions.Regex ___rx = new System.Text.RegularExpressions.Regex(@""& ...
- DataReader和DataSet的异同
DataReader:使用时始终占用SqlConnection,在线操作数据库:每次只在内存中加载一条数据,所以占用的内存是很小的:是只进的. 只读的. DataSet:则是将数据一次性加载在内存中. ...
- pyqt2_官网教程
https://pythonspot.com/en/pyqt4/ Articles You can find a collection of PyQT articles below. Applicat ...
- OC面向对象特性: 继承
基础知识 1.标识符是有字母,数字,下划线组成的. 2.首字母只能是字母,下划线,不能为数字. 3.标识符要做到见名之意. 4.标识符不能使用已定义的关键字和预定义标识符. 继承 继承:子类可以直接访 ...
- ubuntu删除输入法后,循环登陆
在登陆界面ctrl+alt+F1进入tty界面,登陆账号,然后输入 dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P 可以参考Ubuntu1 ...
- Winsock 入门 计算校验和 示例
#include <stdio.h> #include <string.h> #define DATA_MAX_LEN 14 /* 最大数据长度 */ struct data_ ...
- 简单CSS布局留用
1.导航栏固定显示代码,文字居中,z-index header{ position: fixed; top: 0px; left: 10%; width: 80%; height: 80px; bor ...