Android:UI更新方法四:在Worker Thread中runOnUiThread直接刷新UI
16lz
2021-12-04
转自: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运行底层linux外部命令的实现
- Android(安卓)单元测试 Robolectric
- android 执行shell命令代码
- 几个通用的类,迷你型的Android下载框架
- 在Android中使用Timer,并创建一个应用程序
- Android消息机制入门一
- Java乔晓松-android的四大组件之一Service(服务的绑定)
- Mac adb 配置
- linux 下使用ndk-build编译android使用的c++静态库