android開發handler與線程的用法

來源:互聯網
上載者:User

在android的開發中,如果用Handler來啟動Runable函數的run,這都知在主線程中運行,而不是新開一個線程單獨運行,這樣如果run中運行沒有結果就會影響主線程中的Activity,導致應用無響應。所以android就有一個類“HandlerThread”,通過它來建立一個子線程與主線程分開,再把這樣子線程綁定在建立的Handler對象上,這樣run函數就會在子線程中運行,而不影響Activity。

 

        下面例子是線程來控制一個水平進度條的例子:

 

package com.example.taxtbarhandler01;import android.opengl.Visibility;import android.os.Bundle;import android.os.Handler;import android.os.HandlerThread;import android.os.Looper;import android.os.Message;import android.app.Activity;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ProgressBar;public class Taxtnarhandler01 extends Activity {private Button start;private Button end;//進度條private ProgressBar bar;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);start = (Button)findViewById(R.id.start);end = (Button)findViewById(R.id.end);bar = (ProgressBar)findViewById(R.id.progressBar1);bar.setMax(100);//輸出當前線程的名字與ID;System.out.println("Activity-->" + Thread.currentThread().getId());System.out.println("Activity-->" + Thread.currentThread().getName());//start監聽器start.setOnClickListener(new Startlistener());end.setOnClickListener(new Endlistener());}Myhandler handler ;class Startlistener implements OnClickListener{@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stub//輸出當前線程的名字與ID;System.out.println("start-->" + Thread.currentThread().getId());System.out.println("start-->" + Thread.currentThread().getName());bar.setVisibility(View.VISIBLE);//建立一個子線程;HandlerThread updata = new HandlerThread("name");updata.start();//把子線程綁定在handler中handler = new Myhandler(updata.getLooper());handler.post(r);}}class Endlistener implements OnClickListener{@Overridepublic void onClick(View v) {// TODO Auto-generated method stub//輸出當前線程的名字與ID;System.out.println("end-->" + Thread.currentThread().getId());System.out.println("end-->" + Thread.currentThread().getName());bar.setVisibility(View.GONE);}}//匿名內部類class Myhandler extends Handler{public Myhandler(){}public Myhandler(Looper looper){super(looper);}@Override//從訊息佇列中取出訊息處理public void handleMessage(Message msg) {//輸出當前線程的名字與ID;System.out.println("handler-->" + Thread.currentThread().getId());System.out.println("handler-->" + Thread.currentThread().getName());// TODO Auto-generated method stub//更新進度條bar.setProgress(msg.arg1);//進入運行隊列handler.post(r);if (msg.arg1 == 100){handler.removeCallbacks(r);//注意,因為進度條bar是在主線程中建立的,而這個函數是在子線程中運行,所以不能在這裡改變bar的狀態,否則會報錯(這是我個人理解,因為報錯了-_-!)//bar.setVisibility(View.GONE);}}}Runnable r = new Runnable() {int i = 0;@Overridepublic void run() {//輸出當前線程的名字與ID;System.out.println("run-->" + Thread.currentThread().getId());System.out.println("run-->" + Thread.currentThread().getName());// TODO Auto-generated method stubSystem.out.println("++" + i);i = i + 10;try {Thread.sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}//傳送訊息Message msg = handler.obtainMessage();msg.arg1 = i;//訊息入隊列msg.sendToTarget();}};@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}

聯繫我們

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