How can I give a delay of few seconds without using threads.some function that I can call anywhere for giving delay. Android built-in function is highly preferred. Thanks
Call delayed method from a static context
public final class Config {
public static MainActivity context = null;
}
In MainActivity:
@Override
protected void onCreate(final Bundle savedInstanceState) {
...
Config.context = this;
...
}
...
public void execute_method_after_delay(final Callable method, int millisec)
{
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
try {
method.call();
}
catch (Exception e) {
}
}
}, millisec);
}
From any class using static methods:
private static void a_static_method()
{
int delay = 3000;
Config.context.execute_method_after_delay(new Callable() {
public Integer call() {
return method_to_call();
}
}, delay);
}
public static Integer method_to_call()
{
// DO SOMETHING