Wait for 5 seconds

2019-03-15 09:19发布

I tried look for the answer ,found some but did not work for me. I want to wait 5 seconds before starting another public void method. The thread sleep was not working for me.Kindly give your suggestions,and if there is a way of "wait()" without using Threads I would love to know that.

public void check(){
//activity of changing background color of relative layout
}

I want to wait 3 seconds before changing the relative layout color.

标签: android wait
4条回答
小情绪 Triste *
2楼-- · 2019-03-15 09:39

just add one-liner with lambda

(new Handler()).postDelayed(this::yourMethod, 5000);
查看更多
Evening l夕情丶
3楼-- · 2019-03-15 09:39

See if this works for you. Be sure to import the android.os.Handler

      Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                public void run() {
                    // yourMethod();
                }
            }, 5000);   //5 seconds
查看更多
够拽才男人
4楼-- · 2019-03-15 09:40

you can use java handlers to achieve your task:

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    public void run() {
     // Actions to do after 10 seconds
    }
}, 10000);

for more information read the following url:

https://developer.android.com/reference/android/os/Handler.html

查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-03-15 09:42

I have been answering on another post, maybe it could help someone.

How to make Android wait?

查看更多
登录 后发表回答