SZH*_*SZH 2 java android timer scheduled-tasks
在我的Android应用程序中,我想安排一个任务在10秒内运行,除非用户按下特定按钮.做这个的最好方式是什么?我应该使用java.util.Timer,java.util.concurrent.ScheduledThreadPoolExecutor还是其他什么东西?
先感谢您.
如果从任务访问UI,则处理程序适合此:
Runnable runnable = new Runnable() {
public void run () {
// Do your stuff here
}
}
Handler handler = new Handler();
handler.postDelayed(runnable, 10000);
Run Code Online (Sandbox Code Playgroud)
在您的按钮操作中:
handler.removeCallbacks(runnable);
Run Code Online (Sandbox Code Playgroud)
否则,一个Timer很好.
| 归档时间: |
|
| 查看次数: |
1409 次 |
| 最近记录: |