Search code examples
androidmultithreadingandroid-asynctaskrunnable

Android how to implement executing Async task from runnable


The documentation says that execute() must be called from a UI thread. But, since I am updating images every few seconds, I am using a Runnable. And in that, I define the operations that have to be executed. Starting(execute())ASyncTask is one of them. But, since ASyncTask is not supposed to be called from anything but the UI thread, how do I proceed?


Solution

  • just add runOnUiThread in Runnable for starting AsyncTask :

    private Runnable updateImages= new Runnable() {
        public void run() {
            runOnUiThread(new Runnable() {
                public void run() { 
                    // call your asynctask here 
                }
            }
        });
        //////YOUR CODE HERE
    }