Android—定时器的用法
来源:互联网 发布:python获取期货行情 编辑:程序博客网 时间:2024/05/25 20:00
方法一:Handler+Thread
- package com.xunfang.handerDemo;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.widget.TextView;
- /**
- * handler定时器
- *
- * @author Nicholas_Nick
- *
- */
- public class HanderDemoActivity extends Activity {
- TextView tvShow;
- private int i = 0;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- tvShow = (TextView) findViewById(R.id.tv_show);
- new Thread(new ThreadShow()).start();
- }
- // handler类接收数据
- Handler handler = new Handler() {
- public void handleMessage(Message msg) {
- if (msg.what == 1) {
- tvShow.setText(Integer.toString(i++));
- System.out.println("receive....");
- }
- };
- };
- // 线程类
- class ThreadShow implements Runnable {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- while (true) {
- try {
- Thread.sleep(1000);
- Message msg = new Message();
- msg.what = 1;
- handler.sendMessage(msg);
- System.out.println("send...");
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- System.out.println("thread error...");
- }
- }
- }
- }
- }
方法二:Handler类自带的postDelyed
- package com.xunfang.handerDemo;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.widget.TextView;
- /**
- * handler定时器使用postDelyed实现
- *
- * @author Nicholas_Nick
- *
- */
- public class HanderDemoActivity extends Activity {
- TextView tvShow;
- private int i = 0;
- private int TIME = 1000;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- tvShow = (TextView) findViewById(R.id.tv_show);
- handler.postDelayed(runnable, TIME); //每隔1s执行
- }
- Handler handler = new Handler();
- Runnable runnable = new Runnable() {
- @Override
- public void run() {
- // handler自带方法实现定时器
- try {
- handler.postDelayed(this, TIME);
- tvShow.setText(Integer.toString(i++));
- System.out.println("do...");
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- System.out.println("exception...");
- }
- }
- };
- }
方法三:
Handler+Timer+TimerTask
- package com.xunfang.handerDemo;
- 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.widget.TextView;
- /**
- * 定时器实现:Handler+Timer+TimerTask
- *
- * @author Nicholas_Nick
- *
- */
- public class HanderDemoActivity extends Activity {
- TextView tvShow;
- private int i = 0;
- private int TIME = 1000;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- tvShow = (TextView) findViewById(R.id.tv_show);
- timer.schedule(task, 1000, 1000); // 1s后执行task,经过1s再次执行
- }
- Handler handler = new Handler() {
- public void handleMessage(Message msg) {
- if (msg.what == 1) {
- tvShow.setText(Integer.toString(i++));
- }
- super.handleMessage(msg);
- };
- };
- Timer timer = new Timer();
- TimerTask task = new TimerTask() {
- @Override
- public void run() {
- // 需要做的事:发送消息
- Message message = new Message();
- message.what = 1;
- handler.sendMessage(message);
- }
- };
- }
1 0
- Android—定时器的用法
- Android定时器的用法
- android定时器的用法总结
- android定时器的用法总结
- Android定时器的用法、结束进程方法
- Android倒计时定时器CountDownTimer的用法
- SetTimer定时器的用法
- SetTimer定时器的用法
- Linux定时器的用法
- C#定时器的用法
- js 定时器的用法
- 定时器的用法
- 定时器的用法
- QT定时器的用法
- C#定时器的用法
- 定时器的多种用法
- 定时器的用法
- 定时器的简单用法
- 使用Gson解析复杂的json数据
- Java对象序列化
- 大型网站架构知识结构
- i.mx257的gpio简要理解
- 什么是符号文件?
- Android—定时器的用法
- HTML学习(六)——表单
- FEC之异或运算应用
- hust训练赛20160330--B - 又见LKity
- 2016SDAU编程练习一1011
- VC++的UNICODE工程一些常用转码
- Epoll模型详解
- VC 获 取 当前程序运行路径的几种方法
- 华为老总任正非给公司患抑郁症员工的一封信