乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      [經(jīng)典]android 消息機(jī)制(二)

       aaie_ 2012-09-03

      接上文。

      角色綜述(回顧):

      (1)UI thread通常就是main thread,而Android啟動(dòng)程序時(shí)會(huì)替它建立一個(gè)MessageQueue。

      (2)需要一個(gè)Looper對(duì)象,來(lái)管理MessageQueue。

      (3)可以構(gòu)造Handler對(duì)象來(lái)push新消息到Message Queue里;或者接收Looper所送來(lái)的消息。

      (4)線程AHandler對(duì)象可以傳遞給別的線程,讓別的線程能送訊息來(lái)給線程A。

      (5)線程AMessage Queue里的消息,只有線程A所屬的對(duì)象可以處理。

       例二、子線程傳遞消息給主線程

      public class Activity2extends Activityimplements OnClickListener{

            Buttonbutton =null;

            TextViewtext =null;

            MyHandlermHandler =null;

            Threadthread ;

            protected void onCreate(Bundle savedInstanceState) {

                   super.onCreate(savedInstanceState);

                   setContentView(R.layout.activity1);        

                   button = (Button)findViewById(R.id.btn);

                   button.setOnClickListener(this);

                   text = (TextView)findViewById(R.id.content);

            }

            public void onClick(View v) {

                   switch (v.getId()) {

                   case R.id.btn:

                          thread =new MyThread();

                          thread.start();

                          break;

                   }            

            }     

            private class MyHandlerextends Handler{             

                   public MyHandler(Looper looper){

                          super(looper);

                   }

                   public void handleMessage(Message msg) {

                          text.setText(msg.obj.toString());

                   }            

            }

            private class MyThreadextends Thread{

                   public void run() {

                          Looper curLooper = Looper.myLooper();

                          Looper mainLooper = Looper.getMainLooper();

                          String msg ;

                          if(curLooper==null){

                                 mHandler =new MyHandler(mainLooper);

                                 msg ="curLooper is null";

                          }else{

                                 mHandler =new MyHandler(curLooper);

                                 msg ="This is curLooper";

                          }

                          mHandler.removeMessages(0);

                          Message m =mHandler.obtainMessage(1, 1, 1, msg);

                          mHandler.sendMessage(m);

                   }            

            }

      }

      說(shuō)明:

      Android會(huì)自動(dòng)替主線程建立Message Queue。子線程沒(méi)有建立Message Queue。所以mHandler屬于主線程。mHandler.sendMessage(m);m消息存入主線程的Message Queue。mainLooper看到Message Queue里有訊息,主線程執(zhí)行到mHandlerhandleMessage()來(lái)處理消息。

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多