Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

初識 Handler:子線程非同步更新 UI 實作

Android 規定只有主線程能更新 UI,因此耗時運算通常得放到子線程,再把結果回傳主線程刷新介面。本範例展示如何以 Handler + Looper + Message 的組合,讓子線程每隔一秒非同步更新標題,同時不影響主線程對按鈕的正常操作。

首先是 main.xml 版面配置:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:Android="https://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <Button  
  8.         android:id="@+id/btnFirst"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="按鈕1" >  
  12.     </Button>  
  13.   
  14.     <Button  
  15.         android:id="@+id/btnSecond"  
  16.         android:layout_width="fill_parent"  
  17.         android:layout_height="wrap_content"  
  18.         android:text="按鈕2" >  
  19.     </Button>  
  20.   
  21. </LinearLayout>  

程式碼如下:

  1. package com.liujin.game;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.os.Handler;  
  6. import android.os.Looper;  
  7. import android.os.Message;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11.   
  12. public class TestHandlerImage extends Activity implements OnClickListener {  
  13.     private Button btnFirst, btnSecond;  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.         btnFirst = (Button) this.findViewById(R.id.btnFirst);//得到按鈕  
  18.         btnSecond = (Button) this.findViewById(R.id.btnSecond);  
  19.         btnFirst.setOnClickListener(this);//設置監控  
  20.         btnSecond.setOnClickListener(this);  
  21.         MyThread myThread = new MyThread();//開啟子線程  
  22.         myThread.start();  
  23.   
  24.     }  
  25.   
  26.     @Override  
  27.     public void onClick(View view) {  
  28.         switch (view.getId()) {  
  29.         case R.id.btnFirst:  
  30.             btnFirst.setText("我點擊按鈕1");  
  31.             btnSecond.setText("等待點擊");  
  32.             break;  
  33.         case R.id.btnSecond:  
  34.             btnFirst.setText("等待點擊");  
  35.             btnSecond.setText("我點擊按鈕2");  
  36.             break;  
  37.         }  
  38.     }  
  39.       
  40.     class MyThread extends Thread {  
  41.         TitleEventHandler handler;  
  42.         public int sleepTime=1000;  
  43.         public int Interval=0;  
  44.         public void run() {  
  45.             Looper mainLooper = Looper.getMainLooper();//得到主線程的Looper  
  46.             handler = new TitleEventHandler(mainLooper);  
  47.             handler.removeMessages(0);  
  48.             Message msg = null;  
  49.             Long start,end;  
  50.             int time = 15;  
  51.             while (true) {//定時每秒發送一個消息給主線程  
  52.                 try {  
  53.                    time--;  
  54.                    start=System.currentTimeMillis();  
  55.                    msg = handler.obtainMessage(1, 1, 1, "當前還剩" + (time + 1) + "秒");  
  56.                    handler.sendMessage(msg);//發送消息  
  57.                    end=System.currentTimeMillis();  
  58.                    Interval=(int) (end-start);  
  59.                    if(Interval<sleepTime){//定時睡眠  
  60.                         Thread.sleep(sleepTime-Interval);  
  61.                    }  
  62.                 } catch (InterruptedException e) {  
  63.                 }  
  64.             }  
  65.         }  
  66.     }  
  67.   
  68.     class TitleEventHandler extends Handler {  
  69.         public TitleEventHandler() {  
  70.             super();  
  71.         }  
  72.   
  73.         public TitleEventHandler(Looper looper) {  
  74.             super(looper);  
  75.         }  
  76.   
  77.         @Override  
  78.         public void handleMessage(Message msg) {  
  79.             super.handleMessage(msg);  
  80.             setTitle((String) msg.obj);  
  81.         }  
  82.     }  
  83. }  

重點在於:子線程透過 Looper.getMainLooper() 取得主線程的 Looper 來建立 Handler,如此便能將訊息投遞到主線程,再由 handleMessage() 安全地呼叫 setTitle 更新介面,而主線程仍能同時正常響應按鈕點擊。

Copyright © Linux教程網 All Rights Reserved