Android:UI更新方法四:在Worker Thread中runOnUiThread直接刷新UI
来源:互联网 发布:成都程序员招聘 编辑:程序博客网 时间:2024/05/17 04:53
转自:http://blog.csdn.net/annkie/article/details/8496219
Android:UI更新方法四:在Worker Thread中runOnUiThread直接刷新UI
- <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
Activity.java中:
- public final void runOnUiThread(Runnable action) {
- if (Thread.currentThread() != mUiThread) {
- mHandler.post(action);//将Runnable Post到消息队列,由内部的mHandler来处理,实际上也是Handler的处理方式
- } else {
- action.run();//已经在UI线程,直接运行。
- }
- }
0 0
- Android:UI更新方法四:在Worker Thread中runOnUiThread直接刷新UI
- Android:UI更新方法四:在Worker Thread中runOnUiThread直接刷新UI
- [Silverlight] 如何在 Worker thread 中更新 UI
- Android:UI更新方法三:Handler+Worker Thread
- Android在其它线程中更新UI,runOnUiThread() (转载)
- 使用Thread+Handler或runOnUiThread更新UI
- android UI(内容)更新的方法之handler、runOnUiThread()
- runOnUiThread()方法实现在子线程中更新UI组件
- UI Thread & Worker Thread
- Android中更新UI的两种方法——handler和runOnUiThread()
- UI更新方法Handler和runOnUiThread
- 使用runOnUiThread更新UI
- runOnUiThread 用于更新UI
- 使用runOnUiThread更新UI
- 更新UI-runOnUIThread
- runOnUiThread 更新ui
- Android在非UI线程中更新UI的方法
- Android 更新UI——runOnUiThread()
- java、js之间使用AES加密通信数据
- MaptileDownloader1.95安装过程
- 15.Object Manager
- h5离线存储
- TProfiler部署文档--笔记
- Android:UI更新方法四:在Worker Thread中runOnUiThread直接刷新UI
- java websocket网页端客户端实现
- Toolbar
- 关于log4j的一些配置
- 未知来源应用禁止/允许开关
- netbeans 8.0.2 问题或做不到的事
- 从头认识Spring-1.3 Spring的容器与Bean
- Git 命令
- ngrok 反向代理