如何在Android中使用runOnUiThread?


在進入示例之前,我們應該瞭解Android中的runOnUiThread()是什麼。有時主執行緒執行一些繁重的操作。如果使用者想在UI上新增一些額外的操作,它會變得很慢併產生ANR(應用程式無響應)。使用runOnUiThread將在工作執行緒上執行後臺操作並在主執行緒上更新結果。

此示例演示如何在Android中使用runOnUiThread。

步驟1 - 在Android Studio中建立一個新專案,轉到檔案⇒新建專案,並填寫所有必需的詳細資訊以建立新專案。

步驟2 - 將以下程式碼新增到res/layout/activity_main.xml中。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/parent"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity"
   android:gravity="center"
   android:orientation="vertical">
   <Button
      android:id="@+id/runOn"
      android:text="Run"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
   <TextView
      android:id="@+id/text"
      android:textSize="20sp"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
</LinearLayout>

在上面的程式碼中,我們添加了一個按鈕和一個文字檢視,當您單擊按鈕時,它將更新文字檢視。

步驟3 - 將以下程式碼新增到src/MainActivity.java中

package com.example.andy.myapplication;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
   int i = 0;
   @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      final TextView textView = findViewById(R.id.text);
      final Button runOn = findViewById(R.id.runOn);
      runOn.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            new Thread() {
               public void run() {
                  while (i++ < 1000) {
                     try {
                        runOnUiThread(new Runnable() {
                           @Override
                           public void run() {
                              textView.setText("#" + i);
                           }
                        });
                        Thread.sleep(300);
                     } catch (InterruptedException e) {
                        e.printStackTrace();
                     }
                  }
               }
            }.start();
         }
      });
   }
}

在上面的程式碼中,當用戶單擊按鈕時。它將使用runOnUiThread()更新文字檢視,如下所示 -

new Thread() {
   public void run() {
      while (i++ < 1000) {
         try {
            runOnUiThread(new Runnable() {
               @Override
               public void run() {
                  textView.setText("#" + i);
               }
            });
            Thread.sleep(300);
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      }
   }
}.start();

讓我們嘗試執行您的應用程式。我假設您已將您的實際Android移動裝置連線到您的電腦。要從Android Studio執行應用程式,請開啟您的專案中的一個活動檔案,然後單擊工具欄中的執行 圖示。選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示您的預設螢幕 -

在以上結果中,它顯示了初始螢幕。當用戶單擊執行按鈕時,它將更新文字檢視,如下所示 -

點選這裡下載專案程式碼

更新於: 2019年7月30日

5K+ 瀏覽量

啟動您的職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.