Android线程间通信的Message机制
2010-06-01 15:44:00 来源:WEB开发网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);
更多精彩
赞助商链接