Android三种实现定时器的方法

方法一: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 Smalt *  */public class HanderDemoActivity extends Activity {TextView tvShow;private int i = 0;@Overridepublic 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 {@Overridepublic void run() {// TODO Auto-generated method stubwhile (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 blocke.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 Smalt *  */public class HanderDemoActivity extends Activity {TextView tvShow;private int i = 0;private int TIME = 1000;@Overridepublic 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() {@Overridepublic void run() {// handler自带方法实现定时器try {handler.postDelayed(this, TIME);tvShow.setText(Integer.toString(i++));System.out.println("do...");} catch (Exception e) {// TODO Auto-generated catch blocke.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 Smalt *  */public class HanderDemoActivity extends Activity {TextView tvShow;private int i = 0;private int TIME = 1000;@Overridepublic 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() {@Overridepublic void run() {// 需要做的事:发送消息Message message = new Message();message.what = 1;handler.sendMessage(message);}};}


更多相关文章

  1. Android(安卓)使用CoordinatorLayout+AppBarLayout+CollapsingTo
  2. Android之ViewFlipper的使用与ListView的分页加载
  3. 短视频平台源码android四种不同的事件实现
  4. android触摸实现方式
  5. android触摸实现方式
  6. android触摸实现方式
  7. Android中thread.start()出现的thread already exist错误
  8. Android(安卓)禁止转屏的方法
  9. Android:CountDownTimer 实现倒计时功能

随机推荐

  1. 1. php变量的8种数据类型? 2. php变量类型
  2. PHP基础课第二次作业
  3. 作业标题:0803 php变量与常量作业
  4. php变量与常量
  5. 数据类型及类型转换
  6. zy0803
  7. php变量的类型与转换及变量与常量的区别
  8. 第一节课markdown
  9. 【前端 · 面试 】HTTP 总结(四)—— HTTP
  10. php0803 认识变量的作业