Android-初识Handler-子线程异步更新UI


简单的例子:子线程异步的更新UI,同时不影响主线程的操作本身界面的操作
下面是main.xml配置文件

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:Android="http://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,每一个Activity都默认自带一个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(111"当前还剩" + (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. }  

下面是main.xml配置文件

相关内容