Android:UI更新方法四:在Worker Thread中runOnUiThread直接刷新UI
16lz
2021-01-23
转自:http://blog.csdn.net/annkie/article/details/8496219
MainActivity.java:
Android:UI更新方法四:在Worker Thread中runOnUiThread直接刷新UI
activity_main.xml:[html] view plain copy
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context=".MainActivity">
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="50dp"
- android:background="#ff999999"
- android:text="@string/hello_world"/>
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Button"/>
- </LinearLayout>
MainActivity.java:
[java] view plain copy
- packagecom.example.updateui;
- importandroid.os.Bundle;
- importandroid.os.Handler;
- importandroid.os.Message;
- importandroid.app.Activity;
- importandroid.util.Log;
- importandroid.view.Menu;
- importandroid.view.View;
- importandroid.view.Window;
- importandroid.view.View.OnClickListener;
- importandroid.widget.Button;
- importandroid.widget.TextView;
- publicclassMainActivityextendsActivity
- {
- privatestaticfinalStringTAG=MainActivity.class.getSimpleName();
- privatestaticfinalintREFRESH_ACTION=1;
- privateButtonmButton;
- privateTextViewmTextView;
- privateintmCount=0;
- @Override
- protectedvoidonCreate(BundlesavedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mTextView=(TextView)findViewById(R.id.textView1);
- mButton=(Button)findViewById(R.id.button1);
- mButton.setOnClickListener(newOnClickListener()
- {
- @Override
- publicvoidonClick(Viewarg0)
- {
- //TODOAuto-generatedmethodstub
- newThread()//workerthread
- {
- @Override
- publicvoidrun()
- {
- while((mCount<100))
- {
- MainActivity.this.runOnUiThread(newRunnable()//工作线程刷新UI
- {//这部分代码将在UI线程执行,实际上是runOnUiThreadpostRunnable到UI线程执行了
- @Override
- publicvoidrun()
- {
- mCount++;
- mTextView.setText("I'mupdated:"
- +mCount);
- Log.i(TAG,"Count:"+mCount);
- }
- });
- try
- {
- Thread.sleep(1000);
- }
- catch(InterruptedExceptione)
- {
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }
- }
- }
- }.start();
- }
- });
- }
- }
public final voidrunOnUiThread(Runnableaction)
Added in API level 1Runs 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
- publicfinalvoidrunOnUiThread(Runnableaction){
- if(Thread.currentThread()!=mUiThread){
- mHandler.post(action);//将RunnablePost到消息队列,由内部的mHandler来处理,实际上也是Handler的处理方式
- }else{
- action.run();//已经在UI线程,直接运行。
- }
- }
更多相关文章
- Android消息机制入门一
- Android - 多线程 - AsyncTask
- Android利用NotificationListenerService实现消息盒子功能
- Android客户端接收来自Faye的消息推送
- Android 任务、进程和线程
- Android:线程使用来更新UI--Thread、Handler、Looper、TimerTask
- [原]Android应用程序键盘(Keyboard)消息处理机制分析
- Android QQ、微信聊天消息界面设计原理与实现
- Android 消息机制之 MessageQueue 消息队列