计时器--精确到10毫秒(精确度可以自行设定)
来源:互联网 发布:编程达人全套视频 编辑:程序博客网 时间:2024/05/01 23:03
简单实现计时器功能,点击开始按钮开始计时,点击结束按钮计时结束。这只是一个例子,可以根据需要进行修改完善。
布局文件只是简单的TextView和两个Button,这里不再赘述。
package com.example;import java.util.Timer;import java.util.TimerTask;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class MainActivity extends Activity {private int mlCount;private TextView tvTime;protected String time;private Timer timer = null;private TimerTask task = null;private Message msg = null;protected int sec;private Button btStart;private Button btStop;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tvTime = (TextView) findViewById(R.id.tvTime);btStart = (Button)findViewById(R.id.btStart);btStop = (Button)findViewById(R.id.btStop);btStart.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubstart();}});btStop.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubstop();}});}/** * 计时器 */Handler handler = new Handler() {@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stubswitch (msg.what) {case 1:mlCount++;int totalSec = 0;int yushu = 0;// 精确到0.01秒totalSec = (int) (mlCount / 100);yushu = (int) (mlCount % 100);// Set time displayint min = (totalSec / 60); sec = (totalSec % 60);try {// 格式化算出来的时间为String格式“00:00”time = String.format("%1$02d:%2$02d", sec, yushu);tvTime.setText(time);} catch (Exception e) {tvTime.setText("" + min + ":" + sec + ":" + yushu);e.printStackTrace();Log.e("MyTimer onCreate", "Format string error.");}break;default:break;}super.handleMessage(msg);}};/** * 计时器开始计时 */public void start() {if (null == timer) {if (null == task) {task = new TimerTask() {@Overridepublic void run() {// TODO Auto-generated method stubif (null == msg) {msg = new Message();} else {msg = Message.obtain();}msg.what = 1;handler.sendMessage(msg);}};}timer = new Timer(true);// timer.schedule(task, 100, 100); // set timer duration// set timer duration每隔0.01秒刷新一次timer.schedule(task, 10, 10);}}/** * 计时器停止 */public void stop() {task.cancel();task = null;timer.cancel(); // Cancel timertimer.purge();timer = null;handler.removeMessages(msg.what);}}
0 0
- 计时器--精确到10毫秒(精确度可以自行设定)
- VB 6.0 精确到毫秒的计时器 demo
- 时间 精确 到毫秒
- oracle精确到毫秒
- 用formatdatetime精确到毫秒
- java时间精确到毫秒
- java时间精确到毫秒
- 获取时间精确到毫秒
- 时间格式化精确到毫秒
- js 倒计时精确到毫秒
- ORACLE时间精确到毫秒
- 时间戳精确到毫秒
- 获取程序运行时间(精确到毫秒)
- 两个日期之间的工作日时间差(精确到毫秒)
- java Calendar(将时间精确到毫秒)
- 学习备忘--获取当前日期时间(精确到毫秒)
- Linux获取系统当前时间(精确到毫秒)
- 日志时间打印(精确到毫秒)C/C++
- 多线程二 (同步)
- 最大公约数1——迭代法
- aws帐单 2014-08-04 09:00
- 单源最短路—dijkstra算法
- 人脸识别《一》opencv人脸识别之预处理
- 计时器--精确到10毫秒(精确度可以自行设定)
- jobdu1475 非常可乐
- “Spark上流式机器学习算法实现”中期检查报告
- 使用百度地图API根据坐标显示地图
- uva133 The Dole Queue ( 约瑟夫环的模拟)
- dedecms 标题和关键词的调用
- Popular Cows+求强联通量简单题+tarjan算法+POJ
- 参与unity非游戏行业开发者大会小结
- 给 C# 开发者的代码审查清单