Android(安卓)的消息机制(2)
16lz
2021-01-26
上一节中,是主线程自己发了一个消息到自己的Message Queue中,并把消息从队列中提取出来。那么如何由别的线程发送消息给主线程的Message Queue中呢?
直接看代码~~?[Copy to clipboard]Download zuiniuwang.java
- <spanstyle="font-size:14px">/**
- *MessageQueue2.java
- *com.test
- *
- *Function:TODO
- *
- *verdateauthor
- *──────────────────────────────────
- *2011-3-20Leon
- *
- *Copyright(c)2011,TNTAllRightsReserved.
- */
- packagecom.test;
- /**
- *ClassName:MessageQueue2
- *Function:TODOADDFUNCTION
- *Reason:TODOADDREASON
- *
- *@authorLeon
- *@version
- *@sinceVer1.1
- *@Date2011-3-20
- */
- importandroid.app.Activity;
- importandroid.graphics.Color;
- importandroid.os.Bundle;
- importandroid.os.Handler;
- importandroid.os.Looper;
- importandroid.os.Message;
- importandroid.view.View;
- importandroid.view.View.OnClickListener;
- importandroid.widget.Button;
- importandroid.widget.LinearLayout;
- importandroid.widget.TextView;
- publicclassMessageQueue2extendsActivityimplementsOnClickListener{
- privatefinalintWC=LinearLayout.LayoutParams.WRAP_CONTENT;
- privatefinalintFP=LinearLayout.LayoutParams.FILL_PARENT;
- publicTextViewtv;
- privatemyThreadt;
- privateButtonbtn,btn2,btn3;
- publicvoidonCreate(Bundleicicle){
- super.onCreate(icicle);
- LinearLayoutlayout=newLinearLayout(this);
- layout.setOrientation(LinearLayout.VERTICAL);
- btn=newButton(this);
- btn.setId(101);
- btn.setText("testlooper");
- btn.setOnClickListener(this);
- LinearLayout.LayoutParamsparam=
- newLinearLayout.LayoutParams(100,50);
- param.topMargin=10;
- layout.addView(btn,param);
- btn2=newButton(this);
- btn2.setId(102);
- btn2.setText("exit");
- btn2.setOnClickListener(this);
- layout.addView(btn2,param);
- tv=newTextView(this);
- tv.setTextColor(Color.WHITE);
- tv.setText("");
- LinearLayout.LayoutParamsparam2=
- newLinearLayout.LayoutParams(FP,WC);
- param2.topMargin=10;
- layout.addView(tv,param2);
- setContentView(layout);
- }
- publicvoidonClick(Viewv){
- switch(v.getId()){
- case101:
- t=newmyThread();
- t.start();
- break;
- case102:
- finish();
- break;
- }
- }
- //------------------------------------------------------
- classEHandlerextendsHandler{
- publicEHandler(Looperlooper){
- super(looper);
- }
- @Override
- publicvoidhandleMessage(Messagemsg){
- tv.setText((String)msg.obj);
- }
- }
- //------------------------------------------------------
- classmyThreadextendsThread{
- privateEHandlermHandler;
- publicvoidrun(){
- LoopermyLooper,mainLooper;
- //取出本线程中的Looper,当然如果本句话放在主线程中,则会取得主线程的Looper
- myLooper=Looper.myLooper();
- //取出主线程中的Looper
- mainLooper=Looper.getMainLooper();
- Stringobj;
- if(myLooper==null){
- mHandler=newEHandler(mainLooper);
- obj="currentthreadhasnolooper,thismessageisfromMainthread!";
- }
- else{
- mHandler=newEHandler(myLooper);
- obj="Thisisfromnewthread.";
- }
- mHandler.removeMessages(0);
- Messagem=mHandler.obtainMessage(1,1,1,obj);
- mHandler.sendMessage(m);
- }
- }
- }
在本程序中Android仍然会自动替主线程建立Message Queue。在点击按钮之后,新生成的子线程中并不会建立Message Queue。所以,新线程中的myLooper值为null,而mainLooper则指向主线程里的Looper对象。于是,执行当到指令:
mHandler = new EHandler(mainLooper); 时这个mHandler是属于主线程的。
通过指令:mHandler.sendMessage(m); 便将消息m存入到主线程的Message Queue里。mainLooper看到Message Queue里有讯息,就会自动处理,主线程会自动回调mHandler的handleMessage()函数来处理消息。
本文出自 “最牛傻蛋的博客” 博客,转载请与作者联系!
更多相关文章
- Android异步更新UI的方式之使用AsyncTask异步任务
- java 课堂总笔记
- Android多线程下安全访问数据库
- EventBus for Android(安卓)源码分析
- 理解Android核心:Looper, Handler和HandlerThread
- ContentProvider使用难点
- 详解Android(安卓)Handler的使用
- UI与线程交互
- android线程模型文章集合