Skip to content

Instantly share code, notes, and snippets.

@aruke
Last active May 4, 2018 05:07
Show Gist options
  • Save aruke/c66563d13f16dca093b8 to your computer and use it in GitHub Desktop.
Save aruke/c66563d13f16dca093b8 to your computer and use it in GitHub Desktop.
// Call this method directly from java file
int secs = 2; // Delay in seconds
Utils.delay(secs, new Utils.DelayCallback() {
@Override
public void afterDelay() {
// Do something after delay
}
});
import android.os.Handler;
public class Utils {
// Delay mechanism
public interface DelayCallback{
void afterDelay();
}
public static void delay(int secs, final DelayCallback delayCallback){
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
delayCallback.afterDelay();
}
}, secs * 1000); // afterDelay will be executed after (secs*1000) milliseconds.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment