WEB开发网
开发学院手机开发Android 开发 Android线程间通信的Message机制 阅读

Android线程间通信的Message机制

 2010-06-01 15:44:00 来源:WEB开发网   
核心提示:layout.addView(btn5, param);btn6 = new Button(this);btn6.setId(106);btn6.setText("exit");btn6.setOnClickListener(this);layout.addView(btn6, param);tv

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");

上一页  8 9 10 11 12 13 14 15 16  下一页

Tags:Android 线程 通信

编辑录入:coldstar [复制链接] [打 印]
赞助商链接