一、整体工程图
二、activity_handler.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <Button android:text="Handler Lab1" android:id="@+id/buttonHandlerLab1" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button> <Button android:text="Handler Lab2" android:id="@+id/buttonHandlerLab2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button> <Button android:text="Handler Lab3" android:id="@+id/buttonHandlerLab3" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button> <Button android:text="Handler Lab4.1" android:id="@+id/buttonHandlerLab4_1" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button> <Button android:text="Handler Lab4.2" android:id="@+id/buttonHandlerLab4_2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button> <Button android:text="Handler Lab5.1" android:id="@+id/buttonHandlerLab5_1" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button> <Button android:text="Handler Lab5.2" android:id="@+id/buttonHandlerLab5_2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button> </LinearLayout>
package com.jltxgcy.handlerdemo; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.HandlerThread; import android.os.Looper; import android.os.Message; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Toast; import android.os.Process; public class HandlerActivity extends Activity { private static final String TAG="jltxgcy"; private final String HANDLER_KEY = "jltxgcy" ; private Handler lab1Handler1 ; private Handler lab1Handler2 ; private Handler lab2Handler ; private Handler lab3Handler; private Handler lab4Handler; private Handler lab5_1Handler = new Handler(); private Handler lab5_2Handler; private Looper mLooper; private String result; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_handler); lab1Handler2 = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); Toast.makeText(HandlerActivity.this, "[Main Thread]Handler2 Get the message: "+msg.getData().getString(HANDLER_KEY), 5000).show() ; } } ; lab1Handler1 = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); result=msg.getData().getString(HANDLER_KEY); Toast.makeText(HandlerActivity.this, "[Main Thread]Handler1 Get the message: "+msg.getData().getString(HANDLER_KEY), 5000).show() ; } } ; //Handler Lab1 findViewById(R.id.buttonHandlerLab1).setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { new Thread(new Runnable(){ @Override public void run() { lab1Handler1.sendMessage(defineNewMessage("Lab1")) ; //try to use mainHanlder2 to send meesage and receive . //mainHandler2.sendMessage(defineNewMessage("Lab1")) ; } }).start(); } }) ; findViewById(R.id.buttonHandlerLab2).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { new Thread(new Runnable(){ @Override public void run() { lab2Handler = new Handler(Looper.getMainLooper()) { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); Log.d(TAG, "Get the message: "+msg.getData().getString(HANDLER_KEY)+" by Child Thread Handler") ; } }; lab2Handler.sendMessage(defineNewMessage("Lab2")) ; lab3Handler.sendMessage(defineNewMessage("Lab3")) ; } }).start() ; } }); findViewById(R.id.buttonHandlerLab3).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { new Thread(new Runnable(){ @Override public void run() { // TODO Auto-generated method stub Looper.prepare(); lab3Handler = new Handler(){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); Log.d(TAG, "Get the message from "+msg.getData().getString(HANDLER_KEY)+" by Child Thread Handler") ; } } ; Looper.loop(); } }).start() ; } }); findViewById(R.id.buttonHandlerLab4_1).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { HandlerThread thread = new HandlerThread("ServiceStartArguments", Process.THREAD_PRIORITY_BACKGROUND); thread.start(); // Get the HandlerThread's Looper and use it for our Handler mLooper = thread.getLooper(); lab4Handler = new Handler(mLooper){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); Log.d(TAG, "Start message "+msg.getData().getString(HANDLER_KEY)+" by Child Thread Handler") ; try { Thread.sleep(5*1000); } catch (InterruptedException e) { e.printStackTrace(); } Log.d(TAG, "Finish message "+msg.getData().getString(HANDLER_KEY)+" by Child Thread Handler"); } }; lab4Handler.sendMessage(defineNewMessage("Lab4.1")) ; lab4Handler.sendMessage(defineNewMessage("Lab4.2")) ; } }); findViewById(R.id.buttonHandlerLab4_2).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { lab4Handler.removeMessages(1); } }); findViewById(R.id.buttonHandlerLab5_1).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { lab5_1Handler.post(new MyRunnable()); Log.d(TAG, ""+Thread.currentThread().getId()); } }); findViewById(R.id.buttonHandlerLab5_2).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { HandlerThread handlerThread = new HandlerThread("myHandlerThread"); handlerThread.start(); lab5_2Handler = new Handler(handlerThread.getLooper()); lab5_2Handler.post(new MyRunnable()); Log.d(TAG,""+Thread.currentThread().getId()); } }); } private class MyRunnable implements Runnable { public void run() { Log.d(TAG, "Runnable---The Thread is running"); Log.d(TAG, ""+Thread.currentThread().getId()); } } private Message defineNewMessage(String messageContent) { Message returnMsg = new Message() ; Bundle data = new Bundle() ; data.putString(HANDLER_KEY, messageContent) ; returnMsg.setData(data) ; returnMsg.what=1; return returnMsg ; } }
Handler Lab1,接受消息的Handler在主线程中定义,而且处理消息也在主线程中。发送消息任何线程中,本例在子线程中。
Handler Lab2,接受消息的Handler在子线程中定义,而处理消息却在主线程中。发送消息任何线程中,本例在子线程中。
Handler Lab3,接受消息的Handler在子线程中定义,而且处理消息也在子线程中。发送消息任何线程中,本例在主线程中。
Hander Lab4_1,接受消息的Handler在主线程中定义,而处理消息却在子线程中。发送消息任何线程中,本例在主线程中。
Hander Lab4_2,连续发送两条信息,结果如下,说明是一条信息结束后,才处理另一个信息。
如果在发送第一个消息没有Finish前,按下Hander Lab4_2,那么第一个消息不会取消,第二个消息会取消。
Hander Lab5_1,延迟2秒钟,开启run中的方法。结果显示如下,说明没有开启新的线程。
Hander Lab5_2,结果显示如下,这回开启了新的线程。
1、Hander子线程用于每隔多长时间,或者完成一项任务向主线程发送消息,主线程接受消息后,更新UI。
2、Hander主线程负责向子线程发送消息,子线程接受消息后开始下载。
五、Message
获取Message的两种方式,第一种比较易读,第二种比较简洁。
1、Message msg =new Message(); msg.arg1=x; msg.arg2=x; msg.obj=x; msg.replyTo=x; msg.what=x; msg.setData(Bundle bundle); 2、Message msg =Handler.obtain(Handler h, int what, int arg1, int arg2, Object obj); msg.replyTo=x; msg.setData(Bundle bundle); msg.sendToTarget();//不用再用Handler发送了
代码地址:https://github.com/jltxgcy/Demo
六、新增用法
handler.postDelayed()
handler.sentMessageDelayed()
view.postDelayed() //用于延迟更新UI,因为宽度和高度还没有显示出来,延迟后才能显示出来
无论是Message处理函数,还是Runnable对象 都是在主线程中
如果在上面两个函数中再次调用此函数,就相当于每隔多长时间触发一次操作
handler.sentMessage()//不用说了
view.post() //用于子线程中的view更新ui