Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 604
Namratha Avatar asked Apr 02 '26 03:04

Namratha


2 Answers

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
}
like image 56
ρяσѕρєя K Avatar answered Apr 03 '26 18:04

ρяσѕρєя K


Possibly redesign your project to work with only AsyncTasks rather than a Runnable. I'm not sure how AsyncTask likes this behavior as well, but I have changed some UI stuff in AsyncTasks before.

like image 36
Jack Satriano Avatar answered Apr 03 '26 18:04

Jack Satriano