使用runOnUiThread更新UI

来源:互联网 发布:centos搭建l2tp服务器 编辑:程序博客网 时间:2024/05/16 13:05

转载自:http://blog.csdn.net/u010142437/article/details/17953495

下面介绍一种使用Activity的runOnUiThread方法来实现同样的功能,仍以此示例为例:

MainActivity:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.home.testrunonuithread;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.Dialog;  
  5. import android.app.ProgressDialog;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10.   
  11. public class MainActivity extends Activity {  
  12.     // 该程序模拟填充长度为100的数组  
  13.     private int[] data = new int[100];  
  14.     private int hasData = 0;  
  15.     // 定义进度对话框的标识  
  16.     private final int PROGRESS_DIALOG = 0x112;  
  17.     // 记录进度对话框完成的百分比  
  18.     private int progressStatus = 0;  
  19.     // 定义一个进度对话框对象  
  20.     private ProgressDialog pd;  
  21.   
  22.     @Override  
  23.     protected void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.main);  
  26.         Button btn = (Button) findViewById(R.id.btn1);  
  27.         btn.setOnClickListener(new OnClickListener() {  
  28.   
  29.             @Override  
  30.             public void onClick(View v) {  
  31.                 progressStatus = 0;  
  32.                 hasData = 0;  
  33.                 // 显示指定对话框  
  34.                 showDialog(PROGRESS_DIALOG);  
  35.             }  
  36.         });  
  37.     }  
  38.   
  39.     // 创建对话框  
  40.     @Override  
  41.     protected Dialog onCreateDialog(int id, Bundle args) {  
  42.         switch (id) {  
  43.         case PROGRESS_DIALOG:  
  44.             // 创建进度对话框  
  45.             pd = new ProgressDialog(this);  
  46.             pd.setMax(100);  
  47.             // 设置对话框标题  
  48.             pd.setTitle("任务完成百分比");  
  49.             // 设置对话框显示的内容  
  50.             pd.setMessage("下载完成的百分比");  
  51.             // 设置对话框不能用取"消按"钮关闭  
  52.             pd.setCancelable(false);  
  53.             // 设置对话框的进度条风格  
  54.             // pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);  
  55.             pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
  56.             // 设置对话框的进度条是否显示进度  
  57.             pd.setIndeterminate(false);  
  58.             break;  
  59.         }  
  60.         return pd;  
  61.     }  
  62.   
  63.     // 该方法将在onCreateDialog调用后被回调  
  64.     @Override  
  65.     protected void onPrepareDialog(int id, Dialog dialog) {  
  66.   
  67.         super.onPrepareDialog(id, dialog);  
  68.         switch (id) {  
  69.         case PROGRESS_DIALOG:  
  70.             // 对话框进度清零  
  71.             pd.incrementProgressBy(-pd.getProgress());  
  72.             new Thread() {  
  73.                 public void run() {  
  74.                     while (progressStatus < 100) {  
  75.                         // 获取耗时任务完成的百分比  
  76.                         progressStatus = doWork();  
  77.                         // 运行于UI线程  
  78.                         runOnUiThread(new Runnable() {  
  79.   
  80.                             @Override  
  81.                             public void run() {  
  82.                                 // 设置进度条的进度  
  83.                                 pd.setProgress(progressStatus);  
  84.                             }  
  85.                         });  
  86.                     }  
  87.                     // 如果任务已经完成  
  88.                     if (progressStatus >= 100) {  
  89.                         // 关闭对话框  
  90.                         pd.dismiss();  
  91.                     }  
  92.                 }  
  93.   
  94.             }.start();  
  95.             break;  
  96.         }  
  97.     }  
  98.   
  99.     /** 
  100.      * 模拟一个耗时的操作 
  101.      *  
  102.      * @return 
  103.      */  
  104.     public int doWork() {  
  105.         // 为数组元素赋值  
  106.         data[hasData++] = (int) (Math.random() * 100);  
  107.         try {  
  108.             Thread.sleep(100);  
  109.         } catch (InterruptedException e) {  
  110.             e.printStackTrace();  
  111.         }  
  112.         return hasData;  
  113.     }  
  114. }  

布局文件只有一个按钮不再给出。

从runOnUiThread的源码可以看出:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public final void runOnUiThread(Runnable action) {  
  2.         if (Thread.currentThread() != mUiThread) {  
  3.             mHandler.post(action);  
  4.         } else {  
  5.             action.run();  
  6.         }  
  7.     }  


程序首先会判断当前线程是否是UI线程,如果是就直接运行,如果不是则post,这时其实质还是使用的Handler机制来处理线程与UI通讯。

但这个方法,在处理一些简要的更新UI操作时(比如更新adapter等),显得更简洁、方便。


0 0