Android 中的 Handler 是什麼?


我們不能直接將後臺執行緒觸控到主執行緒,所以 Handler 會將主執行緒中所有可用的事件收集到一個佇列中,並將此佇列傳遞給 Looper 類。

在 Android 中,Handler 主要用於從後臺執行緒或其他非主執行緒更新主執行緒。Handler 中有兩個方法。

  • post() - 它將使用 Looper 將訊息從後臺執行緒釋出到主執行緒。

  • sendMessage() - 如果你想組織你傳送到 UI 的內容(來自後臺執行緒的訊息)或 UI 函式,你應該使用 sendMessage()。

此示例演示瞭如何在進度對話方塊中使用 Handler。

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

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

<?xml version = "1.0" encoding = "utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
   xmlns:app = "http://schemas.android.com/apk/res-auto"
   xmlns:tools = "http://schemas.android.com/tools"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   tools:context = ".MainActivity">
   <Button
      android:id = "@+id/button"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"
      android:text = "Click"
      app:layout_constraintBottom_toBottomOf = "parent"
      app:layout_constraintLeft_toLeftOf = "parent"
      app:layout_constraintRight_toRightOf = "parent"
      app:layout_constraintTop_toTopOf = "parent" />
</android.support.constraint.ConstraintLayout>

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

import android.app.ProgressDialog;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
   Handler mHandler;
   ProgressDialog mProgressBar;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Button button = findViewById(R.id.button);
      button.setOnClickListener(this);
   }
   @RequiresApi(api = Build.VERSION_CODES.O)
   @Override
   public void onClick(View v) {
      switch (v.getId()) {
         case R.id.button:
         notificationDialog();
         break;
      }
   }
   @RequiresApi(api = Build.VERSION_CODES.O)
   private void notificationDialog() {
      mHandler=new Handler();
      mProgressBar= new ProgressDialog(MainActivity.this);
      mProgressBar.setMax(100);
      mProgressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
      mProgressBar.show();
      new Thread(new Runnable() {
         @Override
         public void run() {
            for (int i = 0; i <= 100; i++) {
               final int currentProgressCount = i;
               try {
                  Thread.sleep(50);
               } catch (InterruptedException e) {
                  e.printStackTrace();
               }
               //Update the value background thread to UI thread
               mHandler.post(new Runnable() {
                  @Override
                  public void run() {
                     mProgressBar.setProgress(currentProgressCount);
                  }
               });
            }
         }
      }).start();
   }
}

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

Clicl Icon

當用戶單擊上面的按鈕時,它將顯示如下所示的進度對話方塊。

20% Installed


44% Installed

點選 這裡 下載專案程式碼

更新於: 2019-07-30

12K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.