2011-10-28

使用 Android 百分比進度對話窗 ProgressDialog

看過 使用 Android 繞圈進度對話窗 ProgressDialog 之後,再來看看百分比進度對話窗。

原則上兩種對話窗使用大同小異,差異在百分比進度對話窗增加 setMax(...) 與 setProgress(...) 兩個 API 的使用。

主要程式一樣是兩隻:main activity 與 worker thread,以及一隻紀錄 thread 的工具程式。

ProgressActivity
public class ProgressActivity extends Activity {

    private static final String TAG = "ProgressActivity";
    private ProgressDialog progressDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // 建立並顯示進度對話窗
        Utils.logThread();
        this.progressDialog = new ProgressDialog(this);
        this.progressDialog.setTitle("執行中");
        this.progressDialog.setMessage("請稍後...");
        this.progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        this.progressDialog.setMax(10);
        this.progressDialog.show();
        // 建立 worker thread,並交付由 main thread 建立的 handler,供 worker thread 稍後與 main thread 溝通
        new Thread(new DoSomethingThread(this.handler)).start();
    }

    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            Utils.logThread();
            // main thread 從 main queue 取得 handler 丟進來的 message
            // 再從該 message 取得 handler 物件
            // 最後呼叫 handler 的 callback method - handleMessage 來改變進度或者關閉進度對話窗
            int step = msg.getData().getInt("step", -1);
            Log.d(TAG, "step -> " + step);
            if (step == -1) {
                progressDialog.dismiss();
            }
            else {
                progressDialog.setProgress(step);
            }
        }
    };
}
DoSomethingThread
public class DoSomethingThread implements Runnable {

 private Handler handler;

 public DoSomethingThread(Handler handler) {
  super();
  this.handler = handler;
 }

 @Override
 public void run() {
  Utils.logThread();
  for (int i = 1; i <= 10; i++) {
   try {
    // 停頓一秒鐘後,通知 main thread 送出進度
    Thread.sleep(1000);
    Message msg = this.handler.obtainMessage();
    msg.getData().putInt("step", i);
    // 丟一個 message 到 main queue 裡,由 main thread 接手處理
    this.handler.sendMessage(msg);
   }
   catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
  // 最後關閉進度對話窗
  // 丟一個 message 到 main queue 裡,由 main thread 接手處理
  this.handler.sendEmptyMessage(0);
 }
}
Utils 程式請參考 使用 Android 繞圈進度對話窗 ProgressDialog

執行紀錄如下:
10-28 08:07:24.132: DEBUG/Utils(543): <main>id: 1, Priority: 5, Group: main
10-28 08:07:24.722: DEBUG/Utils(543): <Thread-11>id: 11, Priority: 5, Group: main
10-28 08:07:25.722: DEBUG/Utils(543): <main>id: 1, Priority: 5, Group: main
10-28 08:07:25.722: DEBUG/ProgressActivity(543): step -> 1
10-28 08:07:26.764: DEBUG/Utils(543): <main>id: 1, Priority: 5, Group: main
10-28 08:07:26.764: DEBUG/ProgressActivity(543): step -> 2
10-28 08:07:27.811: DEBUG/Utils(543): <main>id: 1, Priority: 5, Group: main
10-28 08:07:27.811: DEBUG/ProgressActivity(543): step -> 3
10-28 08:07:28.857: DEBUG/Utils(543): <main>id: 1, Priority: 5, Group: main
10-28 08:07:28.857: DEBUG/ProgressActivity(543): step -> 4
10-28 08:07:29.861: DEBUG/Utils(543): <main>id: 1, Priority: 5, Group: main
10-28 08:07:29.861: DEBUG/ProgressActivity(543): step -> 5
10-28 08:07:30.916: DEBUG/Utils(543): <main>id: 1, Priority: 5, Group: main
10-28 08:07:30.916: DEBUG/ProgressActivity(543): step -> 6
10-28 08:07:31.951: DEBUG/Utils(543): <main>id: 1, Priority: 5, Group: main
10-28 08:07:31.951: DEBUG/ProgressActivity(543): step -> 7
10-28 08:07:32.998: DEBUG/Utils(543): <main>id: 1, Priority: 5, Group: main
10-28 08:07:32.998: DEBUG/ProgressActivity(543): step -> 8
10-28 08:07:34.051: DEBUG/Utils(543): <main>id: 1, Priority: 5, Group: main
10-28 08:07:34.051: DEBUG/ProgressActivity(543): step -> 9
10-28 08:07:35.111: DEBUG/Utils(543): <main>id: 1, Priority: 5, Group: main
10-28 08:07:35.111: DEBUG/ProgressActivity(543): step -> 10
10-28 08:07:35.111: DEBUG/Utils(543): <main>id: 1, Priority: 5, Group: main
10-28 08:07:35.121: DEBUG/ProgressActivity(543): step -> -1

沒有留言:

張貼留言