我希望在执行另一个公共void方法之前等待5秒钟。线程睡眠对我不起作用。如果有一种不使用Threads的wait()
方法,我会很高兴知道。
public void check(){
//activity of changing background color of relative layout
}
我想在更改相对布局颜色之前等待3秒钟。
我希望在执行另一个公共void方法之前等待5秒钟。线程睡眠对我不起作用。如果有一种不使用Threads的wait()
方法,我会很高兴知道。
public void check(){
//activity of changing background color of relative layout
}
我想在更改相对布局颜色之前等待3秒钟。
看看这是否适用于您。确保导入android.os.Handler
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
// yourMethod();
}
}, 5000); //5 seconds
或者Kotlin
Handler().postDelayed({
// yourMethod()
}, 5000)
android.os.Handler
处理程序。您当前正在使用日志处理程序,该处理程序没有postDelay
方法。您还会收到未初始化的错误,因为您必须实现一些方法以使用日志处理程序。 - Jcorretjer只需使用lambda表达式添加一行代码
(new Handler()).postDelayed(this::yourMethod, 5000);
澄清编辑: yourMethod
是指您想在5000毫秒后执行的方法。
你可以使用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
导入方式: import android.os.Handler;
new Handler().postDelayed(new Runnable() {
public void run() {
// yourMethod();
}
}, 5000); // 5 seconds
This works for me:
val handler = Handler() handler.postDelayed({ // your code to run after 2 second }, 2000)
(new Handler()).postDelayed(this::here is your method,2000);
new Handler().postDelayed(() -> check(), 3000);
这是干净而适合阅读的
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
// Your Code
}
}, 5000);
Handler(Looper.getMainLooper()).postDelayed({
// Your Code
}, 5000)
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();