How to use Runnable in Android?

This article teaches how to use Runnable class in Android.

There are two ways to use thread class.
One is directly used in the Thread.

new Thread(new Runnable() { @Override public void run() { //Do things. } }).start();

Another way is create a new class extend the Runnable interface, and implement the run() function.

public class MyClass extends Activity 
    public void onCreate(Bundle savedInstanceState) 
        Handler mHandler = new Handler(Looper.getMainLooper());

        // Do it after 300 ms.
        mHandler.postDelayed(DoThings, 300);

        // Or do it right away.

    private Runnable DoThings = new Runnable() 
        public void run() 
            // Do things.

1 comment: