如何在不使用threads.some函数的情况下延迟几秒钟,我可以在任何地方调用以延迟.Android内置功能是首选.谢谢
Tre*_*vor 18
使用Handler,使用postDelayed()等方法向其发送简单消息或Runnable.
例如,定义Handler对象以接收消息和Runnables:
private Handler mHandler = new Handler();
Run Code Online (Sandbox Code Playgroud)
定义一个Runnable:
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
// Do some stuff that you want to do here
// You could do this call if you wanted it to be periodic:
mHandler.postDelayed(this, 5000 );
}
};
Run Code Online (Sandbox Code Playgroud)
导致Runnable在指定的延迟后发送到处理程序ms:
mHandler.postDelayed(mUpdateTimeTask, 1000);
Run Code Online (Sandbox Code Playgroud)
如果您不希望将Runnable发送给Handler的复杂性,您也可以非常简单地向其发送消息 - 即使是空消息,为了最简单 - 使用方法sendEmptyMessageDelayed().
| 归档时间: |
|
| 查看次数: |
15742 次 |
| 最近记录: |