Handler基本概念: Handler主要用于异步消息的处理:当发出一个消息之后,首先进入一个消息队列,发送消息的函数即刻返回,而另外一个部分逐个的在消息队列中将消息取出,然后对消息进行出来,就是发送消息和接收消息不是同步的处理。 这种机制通常用来处理相对耗时比较长的操作。 Handler 常用方法:
post(Runnable)
postAtTime(Runnable,long)
postDelayed(Runnable long)
sendEmptyMessage(int)
sendMessage(Message)
sendMessageAtTime(Message,long)
sendMessageDelayed(Message,long)
以上post类方法允许你排列一个Runnable对象到主线程队列中, sendMessage类方法, 允许你安排一个带数据的Message对象到队列中,等待更新.
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;
- @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...");
- }
- }
- }
- }
- }
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; @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 Smalt
- *
- */
- 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...");
- }
- }
- };
- }
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; @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 Smalt
- *
- */
- 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);
- }
- };
- }