Android:UI更新方法四:在Worker Thread中runOnUiThread直接刷新UI

来源:互联网 发布:成都程序员招聘 编辑:程序博客网 时间:2024/05/17 04:53
转自:http://blog.csdn.net/annkie/article/details/8496219

 

Android:UI更新方法四:在Worker Thread中runOnUiThread直接刷新UI

activity_main.xml:

[html] view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"  
  6.     tools:context=".MainActivity" >  
  7.   
  8.     <TextView  
  9.         android:id="@+id/textView1"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="50dp"  
  12.         android:background="#ff999999"  
  13.         android:text="@string/hello_world" />  
  14.   
  15.     <Button  
  16.         android:id="@+id/button1"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:text="Button" />  
  20.   
  21. </LinearLayout>  

MainActivity.java:

[java] view plain copy
  1. package com.example.updateui;  
  2.   
  3. import android.os.Bundle;  
  4. import android.os.Handler;  
  5. import android.os.Message;  
  6. import android.app.Activity;  
  7. import android.util.Log;  
  8. import android.view.Menu;  
  9. import android.view.View;  
  10. import android.view.Window;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.Button;  
  13. import android.widget.TextView;  
  14.   
  15. public class MainActivity extends Activity  
  16. {  
  17.     private static final String TAG = MainActivity.class.getSimpleName();  
  18.     private static final int REFRESH_ACTION = 1;  
  19.     private Button mButton;  
  20.     private TextView mTextView;  
  21.     private int mCount = 0;  
  22.   
  23.     @Override  
  24.     protected void onCreate(Bundle savedInstanceState)  
  25.     {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.activity_main);  
  28.         mTextView = (TextView) findViewById(R.id.textView1);  
  29.         mButton = (Button) findViewById(R.id.button1);  
  30.         mButton.setOnClickListener(new OnClickListener()  
  31.         {  
  32.             @Override  
  33.             public void onClick(View arg0)  
  34.             {  
  35.                 // TODO Auto-generated method stub  
  36.                 new Thread() //worker thread  
  37.                 {  
  38.                     @Override  
  39.                     public void run()  
  40.                     {  
  41.                         while ((mCount < 100))  
  42.                         {  
  43.                             MainActivity.this.runOnUiThread(new Runnable() // 工作线程刷新UI  
  44.                                     { //这部分代码将在UI线程执行,实际上是runOnUiThread post Runnable到UI线程执行了  
  45.                                         @Override  
  46.                                         public void run()  
  47.                                         {  
  48.                                             mCount++;  
  49.                                             mTextView.setText("I'm updated:"  
  50.                                                     + mCount);  
  51.                                             Log.i(TAG, "Count:" + mCount);  
  52.                                         }  
  53.                                     });  
  54.                             try  
  55.                             {  
  56.                                 Thread.sleep(1000);  
  57.                             }  
  58.                             catch (InterruptedException e)  
  59.                             {  
  60.                                 // TODO Auto-generated catch block  
  61.                                 e.printStackTrace();  
  62.                             }  
  63.                         }  
  64.                     }  
  65.                 }.start();  
  66.   
  67.             }  
  68.         });  
  69.     }  
  70.   
  71. }  

public final void runOnUiThread (Runnable action)

Added in API level 1

Runs the specified action on the UI thread. If the current thread is the UI thread, then the action is executed immediately. If the current thread is not the UI thread, the action is posted to the event queue of the UI thread.

如果当前线程是UI主线程,则直接执行Runnable的代码;否则将Runnable post到UI线程的消息队列。

Parameters
actionthe action to run on the UI thread

Activity.java中:

[java] view plain copy
  1. public final void runOnUiThread(Runnable action) {  
  2.        if (Thread.currentThread() != mUiThread) {  
  3.            mHandler.post(action);//将Runnable Post到消息队列,由内部的mHandler来处理,实际上也是Handler的处理方式  
  4.        } else {  
  5.            action.run();//已经在UI线程,直接运行。  
  6.        }  
  7.    }  
0 0