需求如下:在做demo的時候,彈出dialog,需要定時先后執行修改ui的動作.
譬如:
第一次 間隔 2s msg:正在下載
第二次 間隔 2s msg:下載完成,開始安裝
第三次 間隔 2s msg:安裝完成
第四次 間隔 1s 關閉dialog.
自定義Timer類如下:
public class CustomTimer extends Timer {
List<Task> taskList = new ArrayList<Task>();
public CustomTimer addTask(Task task) {
int index = taskList.size();
taskList.add(task);
if (index > 0) {
taskList.get(index - 1).setNextTask(task);
}
return this;
}
public void start() {
if (taskList.size() > 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();
}
分享到:
相关推荐
精确的控制时间间隔,避免Timer的时间间隔不准
Delphi通过多个Timer控件执行多任务..rar
Java里timer执行定时任务
1、背景 在C#里关于定时器的类就有3个: (1)定义在System.Windows.Forms里 ...使用此计时器时,请使用控件的Tick事件执行轮询操作,或在指定的时间内显示启动画面。每当 Enabled 属性设置为true且Interval属性大
Android---Service Timer之执行周期任务
C# Timer 中怎么在固定时间和固定周期中调用事件
PB实现同一个窗口多个TIMER事件功能,互不影响同时运行
定时任务 关于定时任务的启动 和管理。在指定的时间内执行一个任务代码,有时间大家可以看看。
这个任务池由timer及timertask组成,支持多任务,可以随时启动,修改,删除,重启任务等操作,请看附件。
windows下使用的定时器 ...2.timeSetEvent有一个缺点:最多只能定时16分钟(1000000毫秒),而且时间定的长了不一定会调用回调函数 经研究,文中包含的定时器可克服上述缺点。希望各位同仁使用愉快!
java定时任务,每天定时执行任务,每天到这个时间点都会执行
timer自定义高精度定时器,在linux、vxworks、windows平台测试OK
c# ScheduleTimer_src 自定义 c# ScheduleTimer_src
多Timer 事件 实例,可以作为多项业务计时需要用。
所有类型的 Java 应用程序一般都需要计划重复执行的任务。企业应用程序需要计划每日的日志或者晚间批处理过程。一个 J2SE 或者 J2ME 日历应用程序需要根据用户的约定计划闹铃时间。不过,标准的调度类 Timer 和 ...
java定时任务调度Timer简单示例代码 ,
vc 定时器 Timer 多媒体定时器 毫秒 ms
本文实例讲述了C#定时器实现自动执行的方法。分享给大家供大家参考。具体实现方法如下: 代码如下: //下面讲一个打开窗体定时执行按钮的东西 private void Form1_Load(object sender, EventArgs e) { System.Timers...
利用jdk中的timer和timertask来实现,每天定时执行的任务。