Reputation:
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.
Upvotes: 39
Views: 130744
Reputation: 166
As the parameterless constructor of the Handler class is now deprecated on Android 11 and above, if you use the above code it will throw a deprecation warning:
'Handler()' is deprecated as of API 30: Android 11.0 (R).
You should now specify the Looper in the constructor via the Looper.getMainLooper() method as following:
Java
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
// Your Code
}
}, 5000);
Kotlin
Handler(Looper.getMainLooper()).postDelayed({
// Your Code
}, 5000)
java - What do I use now that Handler() is deprecated? - Stack Overflow
Upvotes: 1
Reputation: 776
One line in Java 8
new Handler().postDelayed(() -> check(), 3000);
This is clean and nice for reading
Upvotes: 0
Reputation: 5093
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
or Kotlin
Handler().postDelayed({
// yourMethod()
}, 5000)
Upvotes: 97
Reputation: 301
For import use: import android.os.Handler;
new Handler().postDelayed(new Runnable() {
public void run() {
// yourMethod();
}
}, 5000); // 5 seconds
Upvotes: 6
Reputation: 261
what I prefer is
(new Handler()).postDelayed(this::here is your method,2000);
Upvotes: 1
Reputation: 496
just add one-liner with lambda
(new Handler()).postDelayed(this::yourMethod, 5000);
edit for clarification: yourMethod
refers to the method which you want to execute after 5000 milliseconds.
Upvotes: 48
Reputation: 3104
This works for me:
val handler = Handler() handler.postDelayed({ // your code to run after 2 second }, 2000)
Upvotes: 3
Reputation: 917
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();
Upvotes: -1
Reputation:
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
Upvotes: 13