我想等待 5 秒再启动另一个 public void 方法。线程睡眠对我不起作用。如果有一种不使用线程的方法
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)
只需添加带有 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
这对我有用:
val handler = Handler() handler.postDelayed({ // your code to run after 2 second }, 2000)
我更喜欢的是
(new Handler()).postDelayed(this::here is your method,2000);
Java 8 中的一行
new Handler().postDelayed(() -> check(), 3000);
这很干净,很适合阅读
由于 Handler 类的无参数构造函数现已在 Android 11 及更高版本中弃用,因此如果您使用上述代码,将会抛出弃用警告:
自 API 30 起,“Handler()”已弃用:Android 11.0 (R)。
现在,您应该通过 Looper.getMainLooper() 方法在构造函数中指定 Looper,如下所示:
爪哇
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();