private Handler handler=new Handler(new Callback(){ public boolean handleMessage(Message msg){ //在Callback中可以拦截掉message //这里返回true以后就不会再执行下面的那个handleMessage。 if(msg.what==1)return true; return false; } }){ public void handleMessage(Message msg){ //处理消息 } };
Message msg=handler.obtainMessage(); msg.sendToTarget(); Message msg=Message.obtain(handler); msg.sendToTarget(); Message msg=Message.obtain(); handler.sendMessage(msg); 或者手动绑定,使用 msg.setTarget(handler);
下面做了一个例子,handler例子:
class="code_img_closed" src="/Upload/Images/2015031422/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('9cb8d84a-461a-4411-aacb-bc7d1d5211e6',event)" src="/Upload/Images/2015031422/2B1B950FA3DF188F.gif" alt="" />
package com.ac.handlertest; 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.Button; import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener { private Layout layout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initLayout(); initValue(); } private void initLayout() { layout = new Layout(); layout.helloText = (TextView) findViewById(R.id.helloText); layout.button1 = (Button) findViewById(R.id.button1); layout.button2 = (Button) findViewById(R.id.button2); layout.button3 = (Button) findViewById(R.id.button3); layout.button1.setOnClickListener(this); layout.button2.setOnClickListener(this); layout.button3.setOnClickListener(this); } private void initValue() { layout.helloText.setText(""); layout.button1.setText("1.主线程的Handler"); layout.button2.setText("2.普通子线程的Handler"); layout.button3.setText("3.使用HandlerThread"); } private class Layout { TextView helloText; Button button1; Button button2; Button button3; } // 1.主线程的Handler private void method_1() { new Handler().postDelayed(new Runnable() { @Override public void run() { Log.i("msg", "" + Thread.currentThread());//主线程 layout.helloText.setText("因为主线程中默认包含了一个Looper,所以不需要再传入Looper。"); } }, 1000); } // 2.普通子线程的Handler private void method_2() { Thread thread = new Thread(new Runnable() { @Override public void run() { Looper.prepare(); Handler handler = new Handler() { public void handleMessage(Message msg) { Log.i("msg", "" + Thread.currentThread());//子线程myThread1 runOnUiThread(new Runnable() { public void run() { layout.helloText .setText("为普通线程创建Looper的方法如下:在线程run()方法当中先调用Looper.prepare()初始化Looper,然后再run()方法最后调用Looper.loop(),这样我们就在该线程当中创建好Looper。(注意:Looper.loop()方法默认是死循环)。"); } }); }; }; try { Thread.sleep(1000); handler.sendEmptyMessage(0); } catch (InterruptedException e) { e.printStackTrace(); } Looper.loop(); } }); thread.setName("myThread1"); thread.start(); } // 3.使用HandlerThread private void method_3() { HandlerThread handlerThread = new HandlerThread("myThread2"); handlerThread.start(); Handler handler = new Handler(handlerThread.getLooper()) { public void handleMessage(Message msg) { Log.i("msg", "" + Thread.currentThread());//子线程myThread2 runOnUiThread(new Runnable() { @Override public void run() { layout.helloText .setText("而handlerThread可以直接用handlerThread.getLooper()拿到looper,可以不用管looper,可以省事"); } }); } }; handler.sendEmptyMessage(0); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button1: method_1(); break; case R.id.button2: method_2(); break; case R.id.button3: method_3(); break; default: break; } } }MainActivity.java
<LinearLayout 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" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.ac.handlertest.MainActivity" > <ScrollView android:id="@+id/scrollView1" android:layout_width="match_parent" android:layout_height="wrap_content" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button" /> <Button android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button" /> <TextView android:id="@+id/helloText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" /> </LinearLayout> </ScrollView> </LinearLayout>activity_main.xml