标签:
每一个Context都有一个Looper处理各个Handler发送过来的消息,Looper处理消息的函数是:
msg.target.dispatchMessage(msg);
这里msg是Handler传送给Looper的消息,msg绑定了Handler,这样就不会出现Message搞错Handler的情况,在看Handler中的dispatchMessge(..)源码:
/** * Handle system messages here. */ public void dispatchMessage(Message msg) { if (msg.callback != null) { handleCallback(msg); } else { if (mCallback != null) { if (mCallback.handleMessage(msg)) { return; } } handleMessage(msg); } }
这里是处理消息的,细分为三种:
1.先是Message的callback处理消息,如果为空继续
2.在是Handler的callback接口处理消息,如果为空继续
3.最后是处理消息的是Handler的handleMessage(..)方法。
下面的程序主要验证了这三种方式:
public class MainActivity extends AppCompatActivity { private Button btn_send,btn_start,btn_begin; private myHandler handler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_send = (Button) findViewById(R.id.btn_send); btn_start = (Button) findViewById(R.id.btn_start); btn_begin = (Button) findViewById(R.id.btn_begin); func(); btn_send.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { handler.sendEmptyMessageDelayed(10000, 1000); handler.sendEmptyMessageDelayed(10001, 2000); handler.sendEmptyMessageDelayed(10002, 4000); } }); btn_start.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { MyThread(); } }); btn_begin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { /* * 在子线程中发送消息*/ new Thread(new Runnable() { @Override public void run() { Message msg = Message.obtain(); msg.what = 10000; /*注意这里在子线程初始化Handler一定要传入主线程里的Looper,不然会出错, * 这是因为获取消息的Looper是在主线程*/ handler = new myHandler(MainActivity.this.getMainLooper()); handler.sendMessage(msg); } }).start(); } }); } /* (1) to schedule messages and * runnables to be executed as some point in the future;*/ private void func(){ /*定义handler的时候已经实现了Callback接口,重写handlerMessage(..)方法处理消息*/ handler = new myHandler(new Handler.Callback() { @Override //重写接口中的handleMessage(..)方法 public boolean handleMessage(Message msg) { switch (msg.what){ case 10000: System.out.println("执行handler接口回调:10000"); break; case 10001: System.out.println("执行handler接口回调:10001"); break; case 10002: System.out.println("执行handler接口回调:10002"); break; default:break; } //这里如果返回的是true,那么执行完这里,还将执行Handler重写的 //void handleMessage(Message msg)方法。如果返回ture就不执行 return true; } }); } /* * to enqueue an action to be performed on a different thread than your own.*/ private void MyThread(){ Thread t = new Thread(new Runnable() { @Override public void run() { /*初始化Message的时候指定了回调,这里其实是一个子线程,如果指定了Message回调 * 那么这里会优先执行,而不是执行Handler里的回调函数*/ Message msg = Message.obtain(handler, new Runnable() { @Override public void run() {//指定message的回调 try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("执行Message的回调"); } }); msg.what = 10000; handler.sendMessageDelayed(msg,3000); } }); t.start(); } private class myHandler extends Handler{ /* *重写Handler类中的handleMessage(..)方法处理消息 * */ @Override public void handleMessage(Message msg) { switch (msg.what){ case 10000: System.out.println("执行Handler中重写的HandleMessage方法:10000"); break; case 10001: System.out.println("执行Handler中重写的HandleMessage方法:10001"); break; case 10002: System.out.println("执行Handler中重写的HandleMessage方法:10002"); break; default:break; } } public myHandler(Callback callback) { super(callback); } public myHandler(Looper looper) { super(looper); } } }
xml文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.skymaster.hs.review.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send" android:textSize="30sp" android:id="@+id/btn_send" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start" android:textSize="30sp" android:id="@+id/btn_start" android:layout_below="@id/btn_send" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Begin" android:textSize="30sp" android:id="@+id/btn_begin" android:layout_below="@id/btn_start" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout>
标签:
原文地址:http://www.cnblogs.com/ashitaka/p/5909976.html