Android Handler and Message and Timer
来源:互联网 发布:网络出版许可证查询 编辑:程序博客网 时间:2024/06/05 15:13
定义相关变量
private Handler uHandler;private TimerTask task;private Timer timer = new Timer();;
实现相关代码
Handler 部分:用于处理接收到的消息,根据msg.what部分进行相关消息处理,其中消息有来之Timer与线程
uHandler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case 0: // show Begin time when press Play menu bTime.setText("Begin time: " + nowdate.format(new Date())); break; case 1: // show current time while runin is on going cTime.setText("Current time: " + nowdate.format(new Date())); if (runTime >= 3600) { hour ++; runTime = 0; } pTime.setText("Run time: " + hour +":" + runTime/60 + " "); break; case 2: // no video found message Toast.makeText(getApplicationContext(), "No Video found!", Toast.LENGTH_LONG).show(); break; case 3: // video found message Toast.makeText(getApplicationContext(), "Runin video was found, get ready!", Toast.LENGTH_LONG).show(); break; case 4: // Fail conditions Log.e(TAG, "Got Message for Fail"); setRuninResults(1); break; case 5: // Fass conditions setRuninResults(0); break; } } };
Timer 与Message 部分:实现向Handler发送消息,该部分主要为后台运行的部分,与线程类似。
timer.schedule(task, 0, 1000); /* Now start to Counter */ task = new TimerTask(){ @Override public void run() { runTime ++; if (hour >= RuninTime) { Message msg = new Message(); msg.what = 5; uHandler.sendMessage(msg); }else { Message msg = new Message(); msg.what = 1; uHandler.sendMessage(msg); } } };
Thread 线程部分:线程最简单的创建方法,通过线程向Handler发送消息
new Thread(){ @Override public void run(){ if (!findRuninVideo("/mnt")) { Message msg = new Message(); msg.what = 2; uHandler.sendMessage(msg); } else { Message msg = new Message(); msg.what = 3; uHandler.sendMessage(msg); runinVideo = playList.get(0); } Log.d(TAG, "Thread exit success!"); } }.start();
- Android Handler and Message and Timer
- Handler and Message
- Android message handling mechanism (Handler, Looper, MessageQueue and Message)
- Android message handling mechanism (Handler, Looper, MessageQueue and Message)
- android计时器 message+handler; timer+timertask
- Android Hander and Message
- Android Thread and Handler
- Handler vs Timer : fixed-period execution and fixed-rate execution android development
- Android项目开发实战—倒计时[Handler,Timer,TimerTask,Message]
- Android 主线程与子线程的通信 By Handler and Message
- android handler, thread and looper
- looper and handler in android
- Android Mobile Application - Message casting and receiving
- Sending and receiving broadcast message in Android
- android handler计时器 Timer+handler
- Android Dev Intro - Android Looper And Handler
- android---Timer和Handler
- Android Handler 和 Timer
- 手势识别论文与资源(转)
- php中heredoc的使用方法
- 基于PXE的启动WINPE系统
- 行人检测论文集合(转)
- hdu Cows
- Android Handler and Message and Timer
- 数据库游标
- HDU 4397 - Rubik's Cube
- C#泛型<T>说明
- 图像特征(转)
- 叶树:任总喊你回家吃饭 (zz)
- English job interview Q&A
- JDBC获得生成主键的方法
- 图像变换 - Canny算子边缘检测(cvCanny)