Handler handler = new Handler(Looper.getMainLooper()); // get the main thread's looper handler.post(new Runnable() { @Override public void run() { // code to update the UI here } });
Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { // code to run after a delay here } }, 1000); // delay in milliseconds
Handler handler = new Handler(); Runnable periodicTask = new Runnable() { @Override public void run() { // code to run periodically here handler.postDelayed(this, 1000); // reschedule task every second } }; handler.post(periodicTask);These examples all use the android.os.Handler class from the Android OS package.