问题描述
我怎么可以给几秒钟的延迟,而无需使用threads.some功能,我可以在任何地方打电话给延迟。 Android的内置功能是非常preferred。谢谢
How can I give a delay of few seconds without using threads.some function that I can call anywhere for giving delay. Android built-in function is highly preferred. Thanks
推荐答案
使用一个处理程序,以及使用的方法要么发送一个简单的消息或一个Runnable它如postDelayed()。
Use a Handler, and send either a simple message or a Runnable to it using a method such as postDelayed().
例如,定义一个处理对象以接收消息和的Runnable:
For example, define a Handler object to receive messages and Runnables:
private Handler mHandler = new Handler();
定义一个Runnable:
Define a 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 );
}
};
原因在 MS Runnable接口发送到处理程序在指定的延迟后,
:
mHandler.postDelayed(mUpdateTimeTask, 1000);
如果您不希望发送一个Runnable给处理程序的复杂性,你也可以很简单地将消息发送给它 - 即使是空的消息,为最大简单 - 使用方法 sendEmptyMessageDelayed()
。
If you don't want the complexity of sending a Runnable to the Handler, you could also very simply send a message to it - even an empty message, for greatest simplicity - using method sendEmptyMessageDelayed()
.
这篇关于得到的几秒钟的延迟,而无需使用螺纹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!