我试着寻找答案,发现了一些但对我不起作用。我想在开始另一个公共void方法之前等待5秒钟。线程睡眠对我不起作用。请提出你的建议,如果有一种方法可以等待(#)"没有使用线程我很想知道。
public void check(){
//activity of changing background color of relative layout
}
我想在更改相对布局颜色之前等待3秒钟。
答案 0 :(得分:40)
看看这是否适合您。请务必导入android.os.Handler
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
// yourMethod();
}
}, 5000); //5 seconds
答案 1 :(得分:13)
只需添加带有lambda的单行
(new Handler()).postDelayed(this::yourMethod, 5000);
答案 2 :(得分:8)
您可以使用java处理程序来完成任务:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
// Actions to do after 5 seconds
}
}, 5000);
有关更多信息,请阅读以下网址:
https://developer.android.com/reference/android/os/Handler.html
答案 3 :(得分:2)
这对我有用:
val handler = Handler() handler.postDelayed({ // your code to run after 2 second }, 2000)
答案 4 :(得分:0)
我将this answer发布到另一个问题,但这也可能对您有所帮助。
班级:
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;
}
}
接口:
public interface WaitListener {
public void checkCondition();
public void onWaitEnd();
public void onConditionSuccess();
}
用法示例:
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 :(得分:0)
要导入,请使用:import android.os.Handler;
new Handler().postDelayed(new Runnable() {
public void run() {
// yourMethod();
}
}, 5000); //5 seconds
答案 6 :(得分:0)
我为我的android应用程序使用以下代码(与您之前看到的代码相同),我需要等待一些线程才能启动新方法。很好。
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
// yourMethod();
}
}, 5000); //5 seconds
答案 7 :(得分:0)
我更喜欢的是
(new Handler()).postDelayed(this::here is your method,2000);