转自:http://blog.csdn.net/annkie/article/details/8496219

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

activity_main.xml:

[html] view plain copy
  1. <LinearLayoutxmlns: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. <TextView
  8. android:id="@+id/textView1"
  9. android:layout_width="wrap_content"
  10. android:layout_height="50dp"
  11. android:background="#ff999999"
  12. android:text="@string/hello_world"/>
  13. <Button
  14. android:id="@+id/button1"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:text="Button"/>
  18. </LinearLayout>

MainActivity.java:

[java] view plain copy
  1. packagecom.example.updateui;
  2. importandroid.os.Bundle;
  3. importandroid.os.Handler;
  4. importandroid.os.Message;
  5. importandroid.app.Activity;
  6. importandroid.util.Log;
  7. importandroid.view.Menu;
  8. importandroid.view.View;
  9. importandroid.view.Window;
  10. importandroid.view.View.OnClickListener;
  11. importandroid.widget.Button;
  12. importandroid.widget.TextView;
  13. publicclassMainActivityextendsActivity
  14. {
  15. privatestaticfinalStringTAG=MainActivity.class.getSimpleName();
  16. privatestaticfinalintREFRESH_ACTION=1;
  17. privateButtonmButton;
  18. privateTextViewmTextView;
  19. privateintmCount=0;
  20. @Override
  21. protectedvoidonCreate(BundlesavedInstanceState)
  22. {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25. mTextView=(TextView)findViewById(R.id.textView1);
  26. mButton=(Button)findViewById(R.id.button1);
  27. mButton.setOnClickListener(newOnClickListener()
  28. {
  29. @Override
  30. publicvoidonClick(Viewarg0)
  31. {
  32. //TODOAuto-generatedmethodstub
  33. newThread()//workerthread
  34. {
  35. @Override
  36. publicvoidrun()
  37. {
  38. while((mCount<100))
  39. {
  40. MainActivity.this.runOnUiThread(newRunnable()//工作线程刷新UI
  41. {//这部分代码将在UI线程执行,实际上是runOnUiThreadpostRunnable到UI线程执行了
  42. @Override
  43. publicvoidrun()
  44. {
  45. mCount++;
  46. mTextView.setText("I'mupdated:"
  47. +mCount);
  48. Log.i(TAG,"Count:"+mCount);
  49. }
  50. });
  51. try
  52. {
  53. Thread.sleep(1000);
  54. }
  55. catch(InterruptedExceptione)
  56. {
  57. //TODOAuto-generatedcatchblock
  58. e.printStackTrace();
  59. }
  60. }
  61. }
  62. }.start();
  63. }
  64. });
  65. }
  66. }

public final voidrunOnUiThread(Runnableaction)

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
action the action to run on the UI thread

Activity.java中:

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

更多相关文章

  1. Android运行底层linux外部命令的实现
  2. Android(安卓)单元测试 Robolectric
  3. android 执行shell命令代码
  4. 几个通用的类,迷你型的Android下载框架
  5. 在Android中使用Timer,并创建一个应用程序
  6. Android消息机制入门一
  7. Java乔晓松-android的四大组件之一Service(服务的绑定)
  8. Mac adb 配置
  9. linux 下使用ndk-build编译android使用的c++静态库

随机推荐

  1. Android笔记--handler机制
  2. Android(安卓)实现简单的插件化模块化.
  3. Android(安卓)时间字符串 转成UTC时间转
  4. android 模仿百度地图自定义对话框
  5. Android控制手电筒代码,简单易用,不需要任
  6. Android(安卓)SDK Manager无法更新的解决
  7. Android记事本NotePad应用功能拓展(三)
  8. Space在Android里的应用
  9. Android(安卓)renderscript, more info'
  10. Android(安卓)layout xml总结