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

本文探讨了如何在工作线程(Worker Thread)中利用Android的runOnUiThread方法来更新用户界面。该方法确保即使在非UI线程中,也能安全地将更新操作放到UI线程的事件队列中进行执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

activity_main.xml:

<LinearLayout xmlns: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:

package com.example.updateui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity
{
	private static final String TAG = MainActivity.class.getSimpleName();
	private static final int REFRESH_ACTION = 1;
	private Button mButton;
	private TextView mTextView;
	private int mCount = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mTextView = (TextView) findViewById(R.id.textView1);
		mButton = (Button) findViewById(R.id.button1);
		mButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0)
			{
				// TODO Auto-generated method stub
				new Thread() //worker thread
				{
					@Override
					public void run()
					{
						while ((mCount < 100))
						{
							MainActivity.this.runOnUiThread(new Runnable() // 工作线程刷新UI
									{ //这部分代码将在UI线程执行,实际上是runOnUiThread post Runnable到UI线程执行了
										@Override
										public void run()
										{
											mCount++;
											mTextView.setText("I'm updated:"
													+ mCount);
											Log.i(TAG, "Count:" + mCount);
										}
									});
							try
							{
								Thread.sleep(1000);
							}
							catch (InterruptedException e)
							{
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
				}.start();

			}
		});
	}

}

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

Activity.java中:

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值