ANDROID_MARS學習筆記_S01原始版_008_Handler(非同步訊息處理機制)

來源:互聯網
上載者:User

標籤:

一、代碼
1.xml
(1)main.xml

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:orientation="vertical" 4     android:layout_width="fill_parent" 5     android:layout_height="fill_parent" 6     > 7  8 <ProgressBar 9     android:id="@+id/bar"10     style="?android:attr/progressBarStyleHorizontal"11     android:layout_width="200dp"12     android:layout_height="wrap_content"13     android:visibility="gone"14     />15 <Button 16     android:id="@+id/startButton"17     android:layout_width="fill_parent" 18     android:layout_height="wrap_content" 19     android:text="start"/>20 </LinearLayout>

 

2.java
(1)TestBarHandler.java

 1 package mars.barhandler; 2  3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.os.Handler; 6 import android.os.Message; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.Button;10 import android.widget.ProgressBar;11 12 public class TestBarHandler extends Activity {13     /** Called when the activity is first created. */14     //聲明控制項變數15     ProgressBar bar = null;16     Button startButton = null;17     @Override18     public void onCreate(Bundle savedInstanceState) {19         super.onCreate(savedInstanceState);20         setContentView(R.layout.main);21         //根據控制項的ID得到代表控制項的對象,並為按鈕設定監聽器22         bar = (ProgressBar)findViewById(R.id.bar);23         startButton = (Button)findViewById(R.id.startButton);24         startButton.setOnClickListener(new ButtonListener());25     }26     //當點擊startButton按鈕時,就會執行ButtonListener的onClick方法27     class ButtonListener implements OnClickListener{28 29         @Override30         public void onClick(View v) {31             // TODO Auto-generated method stub32             bar.setVisibility(View.VISIBLE);33             updateBarHandler.post(updateThread);34         }35         36     }37     //使用匿名內部類來複寫Handler當中的handleMessage方法38     Handler updateBarHandler = new Handler(){39 40         @Override41         public void handleMessage(Message msg) {42             bar.setProgress(msg.arg1);43             Bundle bundle = msg.getData();44             updateBarHandler.post(updateThread);45             System.out.println("test---->" + bundle.getString("test"));46         }47         48     };49     //線程類,該類使用匿名內部類的方式進行聲明50     Runnable updateThread = new Runnable(){51         int i = 0 ;52         @Override53         public void run() {54             System.out.println("Begin Thread" + i);55             i = i + 10 ;56             //得到一個訊息對象,Message類是有Android作業系統提供57             Message msg = updateBarHandler.obtainMessage();58             59             //將msg對象的arg1參數的值設定為i,用arg1和arg2這兩個成員變數傳遞訊息,優點是系統效能消耗較少60             msg.arg1 = i ;61             Bundle bundle = new Bundle();62             bundle.putString("test", "test bundle");63             msg.setData(bundle);64             try {65                 //設定當前顯示睡眠1秒66                 Thread.sleep(1000);67             } catch (InterruptedException e) {68                 // TODO Auto-generated catch block69                 e.printStackTrace();70             }71             //將msg對象加入到訊息佇列當中72             if( i > 100){73                 //如果當i的值為100時,就將線程對象從handler當中移除74                 updateBarHandler.removeCallbacks(updateThread);75                 System.out.println(">>>>>>");76             }else{77                 //把訊息壓入訊息佇列,會觸發handler的handleMessage方法78                 updateBarHandler.sendMessage(msg);79                 System.out.println("<<<<<<");80             }81         }82     };83     class MyThread extends Thread{84         public void run(){85             86         }87     }88     89 }

ps:為什麼線程裡的int i = 0 ;為什麼不會每執行一次線程i就歸0?


ANDROID_MARS學習筆記_S01原始版_008_Handler(非同步訊息處理機制)

聯繫我們

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