上文讲到:
Handler会向MessageQueue以两种方式发送消息:sendMessage 和post 。他们都会加在消息队列中
他们的区别是:
SendMessage
Handler对象使用Post发送的是一个runnable对象,加在自己的线程队列中进行处理。所以他们还是在一个线程中进行处理。
package com.example.handler02;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
public class Handler02 extends Activity {
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_handler02);
handler.post(r); // 使用post发送消息
System.out.println("Activity --->"+Thread.currentThread().getId());
System.out.println("Activity --->"+Thread.currentThread().getName());
}
Runnable r = new Runnable(){
@Override
public void run() {
System.out.println("handler ---> "+Thread.currentThread().getId());
System.out.println("handler name ---> "+Thread.currentThread().getName());
try{
Thread.sleep(10000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
};
}
//通过实验可发现,直接使用handler,将线程类(r)放入到线程队列中时,与Activity是同一个线程,并没有新的线程产生
/*
* 11-17 11:59:45.130: I/System.out(1555): Activity --->1
11-17 11:59:45.134: I/System.out(1555): Activity --->main
11-17 11:59:45.162: I/System.out(1555): handler ---> 1
11-17 11:59:45.162: I/System.out(1555): handler name ---> main
*/
第二种方式:
使用HandlerThread建立一个新的线程对象,并放入到Message Queue中进行处理。最后发送的是一个message对象,会被handler的handleMessage的函数进行处理,会在被发送到的线程中进行处理。
package com.example.handler03;
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.view.Menu;
import android.view.MenuItem;
public class Handler03 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_handler03);
System.out.println(Thread.currentThread().getId());
System.out.println(Thread.currentThread().getName());
// 声明一个handler线程,这里HandlerThread 建立了一个新的线程
HandlerThread handlerThread =new HandlerThread("handler_thread");
handlerThread.start(); // 启动线程。
// 将handler线程绑定到一个消息队列上。
MyHandler myhandler = new MyHandler(handlerThread.getLooper());// 得到handleThread的线程管理器looper
Message msg=myhandler.obtainMessage();
Bundle b= new Bundle();
b.putInt("age", 123);
b.putString("name", "zhangsan");
msg.setData(b);
msg.sendToTarget(); // 将msg发送到目标对象,所谓的目标对象就是生成该对象的handler对象(handlerThread)
}
// MyHandler是一个继承Handler的类,使用构造函数将Looper(Message Queue管理器)将最新的线程放入线程队列
// 复写HandleMessage处理线程信息
class MyHandler extends Handler{
public MyHandler(){
}
public MyHandler(Looper looper) // 需要重写构造函数,将Looper对象放入父类handler的构造函数中,即将hanlder绑定到looper对象的线程上,处理消息队列中的消息
{ // looper是消息对象
super(looper);
}
@Override
public void handleMessage(Message msg) {
System.out.println(Thread.currentThread().getId());
System.out.println(Thread.currentThread().getName());
System.out.println("handlerMessage");
Bundle b= msg.getData();
int age = b.getInt("age");
String name=b.getString("name");
System.out.println(name+"");
}
}
}
/*
11-17 13:17:41.710: I/System.out(3389): 1
11-17 13:17:41.714: I/System.out(3389): main
11-17 13:17:41.742: I/System.out(3389): 146
11-17 13:17:41.742: I/System.out(3389): handler_thread
11-17 13:17:41.746: I/System.out(3389): handlerMessage
11-17 13:17:41.750: I/System.out(3389): zhangsan
*
*
*
* */
使用Java中标准Thread方式:
package com.example.handler02;
import android.app.Activity;
import android.os.Bundle;
public class Handler02_2 extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_handler02);
Thread t = new Thread(thread);
t.start(); // 必须启动线程之后程序才可在新线程中使用
System.out.println("Activity --->"+Thread.currentThread().getId());
System.out.println("Activity --->"+Thread.currentThread().getName());
}
Runnable thread =new Runnable(){
@Override
public void run() {
System.out.println("handler ---> "+ Thread.currentThread().getId());
System.out.println("handler --->"+ Thread.currentThread().getName());
try{
Thread.sleep(10000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
};
}
/*
11-17 12:18:27.746: I/System.out(1914): Activity --->1
11-17 12:18:27.746: I/System.out(1914): Activity --->main
11-17 12:18:27.762: I/System.out(1914): handler ---> 101
11-17 12:18:27.762: I/System.out(1914): handler --->Thread-101
*/
// 通过实验结果可发现,使用Thread启动了一个新的线程,