Sometimes we need to delay the execution of a block of code, for example when retrying a failed operation.
here is how to do it:
XML code:
JAVA code:
//this code will print "this is the delayed code" debug log delayed by 10 seconds Handler handler = new Handler(); //note, better use a member variable to avoid garbage collection handler.postDelayed(new Runnable(){ @Override public void run(){ Log.d("test","this is the delayed code"); //put here the code to be executed } } , 10000 //the delay time in milliseconds, in this example 10 seconds );