Android Message机制
参考链接http://www.cnblogs.com/hnrainll/archive/2012/01/06/2314863.html
package com.test.messagedemo; import android.R.string; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.app.Activity; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import android.support.v4.app.NavUtils; public class MainActivity extends Activity { private Button btn1,btn2,btn3,btn4; private TextView tv1,tv2,tv3,tv4; private static final String TAG = "Thread Id:"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn1=(Button)findViewById(R.id.btn1); btn1.setOnClickListener(listener); tv1=(TextView)findViewById(R.id.tv1); btn2=(Button)findViewById(R.id.btn2); btn2.setOnClickListener(listener); tv2=(TextView)findViewById(R.id.tv2); btn3=(Button)findViewById(R.id.btn3); btn3.setOnClickListener(listener); tv3=(TextView)findViewById(R.id.tv3); btn4=(Button)findViewById(R.id.btn4); btn4.setOnClickListener(listener); tv4=(TextView)findViewById(R.id.tv4); } View.OnClickListener listener=new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.btn1://主线程给自己发送消息 Looper looper; looper=Looper.myLooper();//获取当前线程的Looper,现在为主线程 EventHandler mHander=new EventHandler(looper); mHander.removeMessages(0);//删除队列消息 String obj="This is main thread's message and received by itself!"; Message m=mHander.obtainMessage(1, 1, 1, obj); mHander.sendMessage(m); //mHander.handleMessage(m);//sendMessage后,Handler会自动调用handleMessage break; case R.id.btn2://子线程给主线程发送消息 NoLooperThread noLooperThread=new NoLooperThread(); noLooperThread.start(); break; case R.id.btn3://子线程给自己发送消息 OwnLooperThread ownLooperThread=new OwnLooperThread(); ownLooperThread.start(); break; case R.id.btn4://主线程给子线程发送消息 ReceiveMessageThread receiveMessageThread=new ReceiveMessageThread(); receiveMessageThread.start(); break; default: break; } } }; private class EventHandler extends Handler { public EventHandler(Looper looper)//这个方法必须写下来,否则这个类用不了 { super (looper); } @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub switch (msg.what) { case 1: tv1.setText(msg.obj.toString()); break; case 2: tv2.setText(msg.obj.toString()); case 3: tv3.setText(msg.obj.toString()); break; case 4: tv4.setText(msg.obj.toString()); break; default: break; } } } private class NoLooperThread extends Thread { private EventHandler mNoLooperThreadHandler; @Override public void run() { Looper looper=Looper.getMainLooper(); mNoLooperThreadHandler=new EventHandler(looper); mNoLooperThreadHandler.removeMessages(0); String obj="This is the message from otherThread to MainThread!"; Message m=mNoLooperThreadHandler.obtainMessage(2, 1, 1, obj); mNoLooperThreadHandler.sendMessage(m); //mNoLooperThreadHandler.handleMessage(m); Log.i(TAG, "NoLooperThread id:" + this.getId()); //注意将long型转成String的一个技巧 } } private class OwnLooperThread extends Thread { private EventHandler mOwnLooperEventHandler; @Override public void run() { // TODO Auto-generated method stub Looper.prepare(); Looper looper=Looper.myLooper(); mOwnLooperEventHandler=new EventHandler(looper); mOwnLooperEventHandler.removeMessages(0); String obj="This is message from otherThread and to itself!"; Message m=mOwnLooperEventHandler.obtainMessage(3, 1, 1, obj); mOwnLooperEventHandler.sendMessage(m); //mOwnLooperEventHandler.handleMessage(m); Log.i(TAG, "ownLooperThread id:" + this.getId()); } } private class ReceiveMessageThread extends Thread { private EventHandler mReceiveMessageHandler; @Override public void run() { // TODO Auto-generated method stub Looper.prepare(); Looper looper=Looper.getMainLooper(); mReceiveMessageHandler=new EventHandler(looper); mReceiveMessageHandler.removeMessages(0); String obj="This is message from MainThread to OtherThread!"; Message m=mReceiveMessageHandler.obtainMessage(4, 1, 1, obj); mReceiveMessageHandler.sendMessage(m); Log.i(TAG, "ReceiveLooperThread id:" + this.getId()); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28