---
title: "自定義多任務的Timer,任務按指定間隔時間,順序執行"
author: "ltwoini"
date: "2010-07-28T14:23:04Z"
tags: ["UI"]
categories: ["编程语言"]
slug: "timer"
draft: false
lastmod: "2010-07-28T14:23:04Z"
id: 723351
url: "https://ltwoini.iteye.com/blog/723351"
---

需求如下:在做demo的時候,彈出dialog,需要定時先后執行修改ui的動作.

譬如:

第一次 間隔 2s msg:正在下載

第二次 間隔 2s msg:下載完成,開始安裝

第三次 間隔 2s msg:安裝完成

第四次 間隔 1s 關閉dialog.

自定義Timer類如下:

 

public class CustomTimer extends Timer {  
  List&lt;Task&gt; taskList = new ArrayList&lt;Task&gt;();

  public CustomTimer addTask(Task task) {  
   int index = taskList.size();  
   taskList.add(task);  
   if (index &gt; 0) {  
    taskList.get(index - 1).setNextTask(task);  
   }  
   return this;  
  }

  public void start() {  
   if (taskList.size() &gt; 0) {  
    sched(this, taskList.get(0));  
   }  
  }

  private CustomTimer sched(final CustomTimer currentCustomTimer,  
    final Task currenttask) {  
   final CustomTimer next = new DialogTool().new CustomTimer();  
   currentCustomTimer.schedule(new TimerTask() {  
    @Override  
    public void run() {  
     currenttask.run();  
     if (currenttask.getNextTask() != null) {  
      sched(next, currenttask.getNextTask());  
     }  
    }  
   }, currenttask.getDelay());  
   return currentCustomTimer;  
  }

  public abstract class Task {  
   private Task nextTask;

   public abstract void run();

   public abstract long getDelay();

   public Task getNextTask() {  
    return nextTask;  
   };

   public void setNextTask(Task next) {  
    this.nextTask = next;  
   }  
  }  
 }  
//用法如下:

public static void showProgressDialog2(Context context, String title,  
   String msg, boolean cancelable, final long runtime, int iconId,  
   final String title2, final String msg2, final long closetime) {  
  final ProgressDialog mypDialog = new ProgressDialog(context);  
  mypDialog.setProgressStyle(ProgressDialog.STYLE\_SPINNER); //  
  // 设置ProgressDialog 标题  
  mypDialog.setTitle(title); // 设置ProgressDialog 提示信息  
  mypDialog.setMessage(msg); // 设置ProgressDialog 标题图标  
  mypDialog.setIcon(iconId); // 设置ProgressDialog 的进度条是否不明确  
  mypDialog.setIndeterminate(false); // 设置ProgressDialog 是否可以按退回按键取消  
  mypDialog.setCancelable(cancelable); // 让ProgressDialog 显示  
  mypDialog.show();  
  // final Handler currentHandler = new  
  // EHandler(Looper.getMainLooper(),mypDialog);  
  final Activity activity = (Activity) context;

  // new Timer().schedule(new TimerTask() {  
  // @Override  
  // public void run() {  
  // // Message msg= currentHandler.obtainMessage(1,1,1,msg2);  
  // // currentHandler.sendMessage(msg);  
  // // 試試用runOnUiThread方法  
  // activity.runOnUiThread(new Runnable() {// 對UI的操作放在uiThread里面  
  // public void run() {  
  // mypDialog.setMessage(msg2);  
  // mypDialog.setTitle(title2);  
  // };  
  // });  
  // new Timer().schedule(new TimerTask() {  
  // @Override  
  // public void run() {  
  // mypDialog.dismiss();  
  // }  
  // }, closetime);  
  // }  
  // }, runtime);  
  // test...  
  DialogTool dt = new DialogTool();  
  CustomTimer ct = dt.new CustomTimer();  
  dt.new CustomTimer().addTask(ct.new Task() {  
   @Override  
   public long getDelay() {  
    return runtime;  
   }

   @Override  
   public void run() {  
    activity.runOnUiThread(new Runnable() {  
     public void run() {  
      mypDialog.setMessage(msg2);  
      mypDialog.setTitle(title2);  
     }

    });

   }  
  }).addTask(ct.new Task() {  
   @Override  
   public long getDelay() {  
    return runtime;  
   }

   @Override  
   public void run() {  
    activity.runOnUiThread(new Runnable() {  
     public void run() {  
      mypDialog.setMessage("third message");  
      mypDialog.setTitle("third title");  
     }

    });

   }  
  }).addTask(ct.new Task() {

   @Override  
   public long getDelay() {  
    return closetime;  
   }

   @Override  
   public void run() {  
    mypDialog.dismiss();  
   }

  }).start();

 }