转自: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消息机制入门一
  2. Android - 多线程 - AsyncTask
  3. Android利用NotificationListenerService实现消息盒子功能
  4. Android客户端接收来自Faye的消息推送
  5. Android 任务、进程和线程
  6. Android:线程使用来更新UI--Thread、Handler、Looper、TimerTask
  7. [原]Android应用程序键盘(Keyboard)消息处理机制分析
  8. Android QQ、微信聊天消息界面设计原理与实现
  9. Android 消息机制之 MessageQueue 消息队列

随机推荐

  1. Android中的Notification
  2. 开发遇到的low坑
  3. 安卓课程二十三 ImageView实现适屏和裁剪
  4. Android 技术目标:全栈工程师
  5. android更改暗码
  6. Android保存Serializable数据到本地
  7. Android原生下载管理相关记录
  8. Android > 浅谈获取时间
  9. Android VTS test
  10. Android多渠道打包解决方案(兰贝壳儿)