android之網路編程

來源:互聯網
上載者:User

標籤:線程   需要   綁定   不能   style   void   nbsp   重寫   重要   

注意:

(1)主線程不能夠做耗時的操作,網路請求就是耗時的操作需要放到子線程做。
(2)子線程不能更新控制項的內容(更新Ui)。所以產生了矛盾,解決辦法就是使用Handler(處理訊息機制).

 

Handler:訊息機制的寫法(重要) 

使用Handler的步驟:

1.主線程中建立一個Handler

private Handler handler = new Handler(){    public void handleMessage(android.os.Message msg) {    };};

 

2.重寫handler的handlermessage方法

 

 

3.子線程中建立一個Message對象,將擷取的資料繫結給msg

Message msg = new Message();//另一種方式:Message msg = Messge.obtain;msg.obj = result;

 

4.主線程中的handler對象在子線程中將message發送給主線程

handler.sendMessage(msg);

5.主線程中handlermessage方法接受子線程發來的資料,就可以做更新UI的操作。

android之網路編程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.