开发者

Method Start Delay

I was wandering is their anyway to delay a methods start when it is called. I have an update method and I wanted to add a 3 second delay before 开发者_C百科it is started.

Thanks in Advance


That looks like a good job for a Handler. Create a handler in your activity, and then use it to post a runnable (which should contain your code) :

handler.postDelayed(new Runnable(){
    public void run(){
        // Your code goes here...
    }
}, 3000); // Delay by 3000ms

More information on handlers in the official doc.


Are you thinking of a callback or a straight pause?

You can use Thread.sleep() to introduce a delay, however if your application is single threaded this will introduce a delay to your entire application.

Otherwise, you may want to spawn a thread that executes this method and the thread can sleep for three seconds before proceeding or use a ScheduledThreadPoolExecutor to execute a runnable with a 3 second delay initial delay.


you can simply add a Thread.sleep() command as seen here:

try{
  Thread.sleep(4000);
}  catch (InterruptedException ie)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜