android 定时器
来源:互联网 发布:自动记录运动轨迹软件 编辑:程序博客网 时间:2024/05/16 16:46
定时器的三种方法。
(1)Handler
//开始定时public void startTimer() {//移除MSG_TIMINGmHandler.removeMessages(MSG_TIMING);Message msg = Message.obtain();msg.what = MSG_TIMING;msg.arg1 = TYPE_START;//开始计时mHandler.sendMessage(msg);}//停止计时public void stopTimer() {//移除MSG_TIMINGmHandler.removeMessages(MSG_TIMING);}public static final int MSG_TIMING = 1;public static final int TYPE_START = 1;public static final int TYPE_STOP = 2;private Handler mHandler = new Handler() {private int count = 0;@Overridepublic void handleMessage(Message msg) {int what = msg.what;int type = msg.arg1;if (what == MSG_TIMING) {if (type == TYPE_START) {count = 0;}// UI// UI//延迟1000msmHandler.sendEmptyMessageDelayed(MSG_TIMING, 1000);count++;}}};
(2)Handler和Thread
//开始定时public void startTimer() {mThread = new MyThread();mThread.start();}//停止计时public void stopTimer() {if(mThread != null) {mThread.setStop(true);mThread = null;}}public static final int MSG_TIMING = 1;public static final int TYPE_START = 1;public static final int TYPE_STOP = 2;class MyThread extends Thread {private boolean mStop = false;private int count = 0;public void setStop(boolean stop) {stop = true;}@Overridepublic void run() {while(!mStop) {Message msg = Message.obtain();msg.arg1 = count;msg.what = MSG_TIMING;mHandler.sendMessage(msg);try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}count ++;}}}private MyThread mThread;private Handler mHandler = new Handler() {private int count = 0;@Overridepublic void handleMessage(Message msg) {int what = msg.what;int count = msg.arg1;//UI}};
(3)Timer、TimerTak和Handler
public static final int MSG_TIMER = 0;private Handler mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {int what = msg.what;if (what == MSG_TIMER) {mTimerResult = msg.arg1;mTimingText.setText(Integer.toString(mTimerResult));}};};private Timer mTimer;TimerTask mTask;private int mTimerResult = 0;private boolean mIsTiming = false;private boolean mStarted = false;public void startTimer() {mIsTiming = true;restartTimer();}public void restartTimer() {mHandler.removeMessages(MSG_TIMER);if (mTimer != null) {mTimer.cancel();mTimer = null;}if (mTask != null) {mTask.cancel();mTask = null;}mTimer = new Timer();mTask = new TimerTask() {private int count = 0;@Overridepublic void run() {Message msg = Message.obtain();msg.what = MSG_TIMER;msg.arg1 = count;mHandler.sendMessage(msg);count++;}};mTimer.schedule(mTask, 0, 1000);}public void stopTimer() {mIsTiming = false;mHandler.removeMessages(MSG_TIMER);if (mTimer != null) {mTimer.cancel();mTimer = null;}if (mTask != null) {mTask.cancel();mTask = null;}}
0 0
- Android 定时器
- Android 定时器
- Android 定时器
- Android 定时器
- Android定时器
- android 定时器
- android 定时器
- android 定时器
- Android 定时器
- android 定时器
- android定时器
- android定时器
- android定时器
- android 定时器
- android定时器
- android 定时器
- Android定时器
- Android定时器
- Ubuntu12.04 64位安装qq
- B+树,聚集索引,非聚集索引(辅助索引) 之一
- 科大讯飞 无录音权限
- 亲和数
- 前k小的数求法
- android 定时器
- 不使用sizeof求一个变量的大小
- IF-ERRORLEVEL使用方法
- Zend Framework2 - Layout配置,不同模块Module设置不同的layout
- Latex论文编写教程 计算机相关专业论文编写指南
- 默认EditTextView不自动获取焦点的方法
- 先学习搭建个架子:Eclipse java EE版 + S2SH + Oracle11g + Plsql.(3)
- 排序算法--希尔排序(JAVA)
- 深入理解DB2索引(Index)