android 的Handler類的用法

來源:互聯網
上載者:User
 

   <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent">
    <TextView
    android:id="@+id/instruction"
    android:layout_width="fill_parent"
    android:layout_weight="0.8"
    />
    <TextView
    android:id="@+id/lend"
    android:layout_width="fill_parent"
    android:shadowRadius="0.3"/>
    </LinearLayout>

Handler的使用  線程
runable介面實現的run 函數並沒有啟動新線程,而是在當前線程中執行。
Runnable r=new Runable(){
public void run(){
//程式執行代碼}}
Handler和使用線程的這個activity是在同一個線程當中
private Handler handler=new Handler();
Thread.currentThread().getId();
Thread.currentThread().getName();

這和標準java實現線程是不同的
Thread t=new Thread (r);
t.start();這樣線程和主線程就不同了^_^  (*^__^*) 嘻嘻……
handler.post(r);時並沒有調用線程的start方法
Bundle對象:以string為鍵是一種比較特殊的表map,是一個資料存放區工具

loop類:提供迴圈的從隊列中取得休息。
HandlerThread類就可以迴圈處理等待的線程(訊息佇列)
HandlerThread.getlooper
Handler.handlemassge
handler.obtainMessage
Message.sendToTarget(handler)
 
Message.obj;傳遞少量資料
Message.setData(Bundle)
Bundle b=new Bundle();
b.putInt("age",20);
b.putString("name",joy)O(∩_∩)O哈哈~  不錯不錯
int age=b.getData("name");

使用HandlerThread的getLooper前一定要調用該類的handthread。start();

非同步訊息處理:在新線程中handlemessage
執行前修改下mainfest檔案

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.