Wait for 5 seconds

馋奶兔 提交于 2019-12-20 09:47:08

问题


I want to wait 5 seconds before starting another public void method. The thread sleep was not working for me. 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.


回答1:


just add one-liner with lambda

(new Handler()).postDelayed(this::yourMethod, 5000);



回答2:


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



回答3:


you can use java handlers to achieve your task:

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

for more information read the following url:

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




回答4:


I posted this answer to another question, but it may also help you.

Class:

import android.os.Handler;
import android.os.Looper;

public class Waiter {

WaitListener waitListener;
int waitTime = 0;
Handler handler;
int waitStep = 1000;
int maxWaitTime = 5000;
boolean condition = false;

public Waiter(Looper looper, final int waitStep, final int maxWaitTime){

    handler = new Handler(looper);
    this.waitStep = waitStep;
    this.maxWaitTime = maxWaitTime;

}

public void start(){

    handler.post(new Runnable() {
        @Override
        public void run() {

            waitListener.checkCondition();

            if (condition) {

                waitListener.onConditionSuccess();

            } else {
                if (waitTime <= maxWaitTime) {

                    waitTime += waitStep;
                    handler.postDelayed(this, waitStep);

                } else {

                    waitListener.onWaitEnd();
                }
            }
        }
    });

}

public void setConditionState(boolean condition){
    this.condition = condition;
}

public void setWaitListener(WaitListener waitListener){
    this.waitListener = waitListener;
}

}

Interface:

public interface WaitListener {

public void checkCondition();

public void onWaitEnd();

public void onConditionSuccess();

}

Usage example:

ConnectivityManager mConnMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
final int result = mConnMgr.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, "enableMMS");

final Waiter waiter = new Waiter(getMainLooper(), 1000, 5000);
waiter.setWaitListener(new WaitListener() {

            @Override
            public void checkCondition() {
                Log.i("Connection", "Checking connection...");
                NetworkInfo networkInfo = mConnMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_MMS);
                waiter.setConditionState(networkInfo.isConnected());
            }

            @Override
            public void onWaitEnd() {
                Log.i("Connection", "No connection for sending");
                //DO
            }

            @Override
            public void onConditionSuccess() {
                Log.i("Connection", "Connection success, sending...");
                //DO
            }

});

waiter.start();



回答5:


For import use : import android.os.Handler;

 new Handler().postDelayed(new Runnable() {
            public void run() {
                // yourMethod();
            }
        }, 5000);   //5 seconds



回答6:


I use the following code (the same as you see before) for my android app, i need to wait some threads before start my new method. It works fine.

 Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                // yourMethod();
            }
        }, 5000);   //5 seconds


来源:https://stackoverflow.com/questions/41664409/wait-for-5-seconds

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!