线程 handler

来源:互联网 发布:如何用excel将数据分组 编辑:程序博客网 时间:2024/05/16 08:36
package com.android.messageexample;   
import android.app.Activity;   
import android.content.Context;   
import android.graphics.Color;   
import android.os.Bundle;   
import android.os.Handler;   
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.LinearLayout;   
import android.widget.TextView;   
public class MessageExample extends Activity implements OnClickListener {   
private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT;   
private final int FP = LinearLayout.LayoutParams.FILL_PARENT;   
public TextView tv;   
private EventHandler mHandler;   
private Handler mOtherThreadHandler=null;   
private Button btn, btn2, btn3, btn4, btn5, btn6;   
private NoLooperThread noLooerThread = null;   
private OwnLooperThread ownLooperThread = null;   
private ReceiveMessageThread receiveMessageThread =null;   
private Context context = null;   
private final String sTag = "MessageExample";   
private boolean postRunnable = false;   
  
/** Called when the activity is first created. */  
@Override  
public void onCreate(Bundle savedInstanceState) {   
       super.onCreate(savedInstanceState);   
       context = this.getApplicationContext();   
       LinearLayout layout = new LinearLayout(this);   
       layout.setOrientation(LinearLayout.VERTICAL);   
       btn = new Button(this);   
       btn.setId(101);   
       btn.setText("message from main thread self");   
       btn.setOnClickListener(this);   
       LinearLayout.LayoutParams param =   
         new LinearLayout.LayoutParams(250,50);   
       param.topMargin = 10;   
       layout.addView(btn, param);   
       btn2 = new Button(this);   
       btn2.setId(102);   
       btn2.setText("message from other thread to main thread");   
       btn2.setOnClickListener(this);   
       layout.addView(btn2, param);   
       btn3 = new Button(this);   
       btn3.setId(103);   
       btn3.setText("message to other thread from itself");   
       btn3.setOnClickListener(this);   
       layout.addView(btn3, param);   
       btn4 = new Button(this);   
       btn4.setId(104);   
       btn4.setText("message with Runnable as callback from other thread to main thread");   
       btn4.setOnClickListener(this);   
       layout.addView(btn4, param);   
       btn5 = new Button(this);   
       btn5.setId(105);   
       btn5.setText("main thread's message to other thread");   
       btn5.setOnClickListener(this);   
       layout.addView(btn5, param);   
       btn6 = new Button(this);   
       btn6.setId(106);   
       btn6.setText("exit");   
       btn6.setOnClickListener(this);   
       layout.addView(btn6, param);   
       tv = new TextView(this);   
       tv.setTextColor(Color.WHITE);   
       tv.setText("");   
       LinearLayout.LayoutParams param2 =   
          new LinearLayout.LayoutParams(FP, WC);   
       param2.topMargin = 10;   
       layout.addView(tv, param2);   
       setContentView(layout);       
         
       //主线程要发送消息给other thread, 这里创建那个other thread   
   receiveMessageThread = new ReceiveMessageThread();   
   receiveMessageThread.start();   
}   
  
//implement the OnClickListener interface   
@Override  
public void onClick(View v) {   
   switch(v.getId()){   
   case 101:   
//主线程发送消息给自己   
Looper looper;   
looper = Looper.myLooper();   //get the Main looper related with the main thread   
//如果不给任何参数的话会用当前线程对应的Looper(这里就是Main Looper)为Handler里面的成员mLooper赋值   
mHandler = new EventHandler(looper);   
//mHandler = new EventHandler();   
// 清除整个MessageQueue里的消息   
mHandler.removeMessages(0);   
String obj = "This main thread's message and received by itself!";   
//得到Message对象   
Message m = mHandler.obtainMessage(1, 1, 1, obj);   
// 将Message对象送入到main thread的MessageQueue里面   
mHandler.sendMessage(m);   
break;   
   case 102:   
//other线程发送消息给主线程   
postRunnable = false;   
noLooerThread = new NoLooperThread();   
noLooerThread.start();   
break;   
   case 103:     
//other thread获取它自己发送的消息   
tv.setText("please look at the error level log for other thread received message");   
ownLooperThread = new OwnLooperThread();   
ownLooperThread.start();   
break;   
   case 104:       
//other thread通过Post Runnable方式发送消息给主线程   
postRunnable = true;   
noLooerThread = new NoLooperThread();   
noLooerThread.start();   
break;   
   case 105:       
//主线程发送消息给other thread   
if(null!=mOtherThreadHandler){   
tv.setText("please look at the error level log for other thread received message from main thread");   
String msgObj = "message from mainThread";   
Message mainThreadMsg = mOtherThreadHandler.obtainMessage(1, 1, 1, msgObj);   
mOtherThreadHandler.sendMessage(mainThreadMsg);   
}   
break;   
   case 106:   
finish();   
break;   
   }   
}   
class EventHandler extends Handler   
{   
   public EventHandler(Looper looper) {   
super(looper);   
   }   
   public EventHandler() {   
super();   
   }   
   public void handleMessage(Message msg) {   
//可以根据msg.what执行不同的处理,这里没有这么做   
switch(msg.what){   
case 1:   
tv.setText((String)msg.obj);   
break;   
case 2:   
tv.setText((String)msg.obj);   
noLooerThread.stop();   
break;   
case 3:   
//不能在非主线程的线程里面更新UI,所以这里通过Log打印收到的消息   
Log.e(sTag, (String)msg.obj);   
ownLooperThread.stop();   
break;   
default:   
//不能在非主线程的线程里面更新UI,所以这里通过Log打印收到的消息   
Log.e(sTag, (String)msg.obj);   
break;   
}   
   }   
}   
//NoLooperThread   
class NoLooperThread extends Thread{   
   private EventHandler mNoLooperThreadHandler;   
   public void run() {   
Looper myLooper, mainLooper;   
myLooper = Looper.myLooper();   
mainLooper = Looper.getMainLooper(); //这是一个static函数   
String obj;   
if(myLooper == null){   
mNoLooperThreadHandler = new EventHandler(mainLooper);   
obj = "NoLooperThread has no looper and handleMessage function executed in main thread!";   
}   
else {   
mNoLooperThreadHandler = new EventHandler(myLooper);   
obj = "This is from NoLooperThread self and handleMessage function executed in NoLooperThread!";   
}   
mNoLooperThreadHandler.removeMessages(0);   
if(false == postRunnable){   
//send message to main thread   
Message m = mNoLooperThreadHandler.obtainMessage(2, 1, 1, obj);   
mNoLooperThreadHandler.sendMessage(m);   
Log.e(sTag, "NoLooperThread id:" + this.getId());   
}else{   
//下面new出来的实现了Runnable接口的对象中run函数是在Main Thread中执行,不是在NoLooperThread中执行   
//注意Runnable是一个接口,它里面的run函数被执行时不会再新建一个线程   
//您可以在run上加断点然后在eclipse调试中看它在哪个线程中执行   
mNoLooperThreadHandler.post(new Runnable(){     
    @Override     
    public void run() {     
   tv.setText("update UI through handler post runnalbe mechanism!");   
   noLooerThread.stop();   
    }     
});     
}   
   }   
}   
  
//OwnLooperThread has his own message queue by execute Looper.prepare();   
class OwnLooperThread extends Thread{   
   private EventHandler mOwnLooperThreadHandler;   
   public void run() {   
Looper.prepare();   
Looper myLooper, mainLooper;   
myLooper = Looper.myLooper();   
mainLooper = Looper.getMainLooper(); //这是一个static函数   
String obj;   
if(myLooper == null){   
mOwnLooperThreadHandler = new EventHandler(mainLooper);   
obj = "OwnLooperThread has no looper and handleMessage function executed in main thread!";   
}   
else {   
mOwnLooperThreadHandler = new EventHandler(myLooper);   
obj = "This is from OwnLooperThread self and handleMessage function executed in NoLooperThread!";   
}   
mOwnLooperThreadHandler.removeMessages(0);   
//给自己发送消息   
Message m = mOwnLooperThreadHandler.obtainMessage(3, 1, 1, obj);   
mOwnLooperThreadHandler.sendMessage(m);   
Looper.loop();   
   }   
}   
  
//ReceiveMessageThread has his own message queue by execute Looper.prepare();   
class ReceiveMessageThread extends Thread{   
   public void run() {   
Looper.prepare();   
mOtherThreadHandler = new Handler(){   
public void handleMessage(Message msg) {   
    Log.e(sTag, (String)msg.obj);   
}   
};   
Looper.loop();   
   }   
}   
  
}  
原创粉丝点击