ChatGPT解决这个技术问题 Extra ChatGPT

Return a value from AsyncTask in Android [duplicate]

This question already has answers here: How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class? (17 answers) Closed 5 years ago.

One simple question: is it possible to return a value in AsyncTask?

//AsyncTask is a member class
private class MyTask extends AsyncTask<Void, Void, Void>{

    protected Void doInBackground(Void... params) {
         //do stuff
         return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        //do stuff
        //how to return a value to the calling method?
    }
}

And then within my Activity/Fragment:

// The task is started from activity
myTask.execute()
// something like this?
myvalue = myTask.getvalue() 

EDIT: This was asked a long time ago where I wasn't familiar with Java, now that I'm better with it, I 'll do a quick summary:

The point of async task is that the task is asynchronous, meaning that after you call execute() on the task, the task starts running on a thread of its own. returning a value from asynctask would be pointless because the original calling thread has already carried on doing other stuff (thus the task is asynchronous).

Think of time: At one point of time, you started a task that will run in parallel with the main thread. When the parallel-running task completed, time has also elapsed on the main thread. The parallel task cannot go back in time to return a value to the main thread.

I was coming from C so I didn't know much about this. But it seems that lots of people are having the same question so I thought I would clear it up a bit.

With regards to your edits; a closely related set of thread based objects do have the concept of returning something; threadpools and futures. When you submit a task to a thread pool you get a future. At some future time (hopefully after the runnable has finished) you ask the future for its return value. If the runnable has finished you immediately get the return value, if not the thread waits until it has finished. I often use this to spit something into several parts, run them all concurrently and then get the results (often only a few lines later)
@RichardTingle can you provide me some link about what you is talking about...sample code would be better. Thanks in advance & plzz don't forget to mention me in your comment .
Can I get a result with something like a listener?
Yes, but be careful not to leak it, use a WeakReference or something similar

T
Ted Hopp

That's what onPostExecute() is for. It runs on the UI thread and you can deliver your result from there to the screen (or anywhere else you need). It won't be called until the final result is available. If you want to deliver intermediate results, take a look at onProgressUpdate()


yes i know doInBackground() returns data and puts it there, but how do i transfer the data to my main activity in a variable form?
@Tom91136 - Just override onPostExecute() to store the result in a variable of your choice. Note that your original code doesn't make sense because the (hypothetical) method myTask.getValue() would be called before a result was available. You can also call AsyncTask's get() method to obtain the result, but you shouldn't do this from the UI thread until you know for sure that the result is available. Otherwise, it will block the UI thread, defeating the purpose of using AsyncTask in the first place.
@TedHopp you say to store the result in a variable of your choice in onPostExecute() but how do you get that variable BACK to your activity? For example, if you want your task to connect to the internet and download some information and then you want to do something with that information... How do you .execute() the AsyncTask and then do something with that information if the next line of code runs before the AsyncTask is done?
@Jakobud - You can't do that. You need to move that "next line of code" somewhere else (e.g., onPostExecute()). I use this analogy: you don't write code that waits for user input (e.g., a button press); instead, you write event handlers that react when the user provides some input. Think of onPostExecute() as an event handler for when the results of the AsyncTask are available. That's where you put the code (or call the methods) that won't work unless the results are, indeed, available.
So the AsyncTask shouldn't really be thought of as a utility function that simply fetches and returns some information but rather something much larger that fetches info and also manipulates the UI (or whatever) after fetching that info?
M
Meneer Venus

Why not call a method that handles the value?

public class MyClass extends Activity {

    private class myTask extends AsyncTask<Void, Void, Void> {

        //initiate vars
        public myTask() {
            super();
            //my params here
        }

        protected Void doInBackground(Void... params) {
            //do stuff
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            //do stuff
            myMethod(myValue);
        }
    }

    private myHandledValueType myMethod(Value myValue) {
        //handle value 
        return myHandledValueType;
    }
}

and how do i access myMethod?
It not work, e.g. myTask mTask = new myTask(); mTask.execute(); will not return the myHandledValueType.
The formatting is poor, but basically the AsyncTask calls the activity method: myMethod with myValue (which supposedly is a class variable that gets set in doInBackground). In myMethod you process the result of the AsyncTask (there is No reason for a return value here, just do the work in this method for the result). A better implementation would be to extend AsyncTask and have doInBackground return Value and have OnPostExecute take in the Value and pass it to myMethod - this would avoid any messy class variables and is how AsyncTask is designed to work.
I used asynctask with an interface. The interface gets triggered when asynctask is completed (and is successful in my case, successful in the sense upload success). In the success function of interface I called the method from asynctask and I got the value
An issue with this approach is that the AsyncTask will be triggered (repeatedly) if there is a configuration change in the activity and the task is started from one of the lifecycle methods... learned this the hard way had multiple tasks doing the same thing upon changing the screen orientation
S
Sheamus O'Halloran

Easiest way is to pass the calling object into the async task (upon constructing it if you like):

public class AsyncGetUserImagesTask extends AsyncTask<Void, Void, Void> {

    private MyImagesPagerFragment mimagesPagerFragment;
    private ArrayList<ImageData> mImages = new ArrayList<ImageData>();

    public AsyncGetUserImagesTask(MyImagesPagerFragment imagesPagerFragment) {
        this.mimagesPagerFragment = imagesPagerFragment;
    }

    @Override
    public Void doInBackground(Void... records) {
        // do work here
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        mimagesPagerFragment.updateAdapter(mImages);
    }
}

And the in the calling class (your activity or fragment) execute the task:

public class MyImagesPagerFragment extends Fragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        AsyncGetUserImagesTask mGetImagesTask = new AsyncGetUserImagesTask(this);
        mGetImagesTask.execute();
    }

And then the onPostExecuteMethod will call any method on your originating class you like, eg:

    public void updateAdapter(List<ImageData> images) {
        mImageAdapter.setImages(images);
        mImageAdapter.notifyDataSetChanged();
    }
}

this is essentially dependency injection
This solution works. Unless this is going to be reusable by other fragments, it would be simpler to just make the async task a private inner class (or anonymous) and not pass around the fragment. Also instead of using Void for results your could specify the image array. doInBackground would return the image array, and on post execute would take it in as a parameter, this would eliminate the need for a class variable to pass data around.
@jt-gilkeson that's correct but that wouldn´t be reusable either as you would have to include that inner class in every Activity class you have. How can that be achieved? to make a reusable AsyncTask class?
@DIEGOF.G. Correct - hence the "Unless this is going to be reusable" qualification. If you wanted a reusable solution - you could create an custom fragment class with the inner asynctask class embedded - then extend fragments that need this functionality from that class.
Or you could make the AsyncGetUserImagesTask constructor take in an interface instead of a concrete fragment - then anything that implements the interface could use the class (i.e. dependency injection).
j
jt-gilkeson

Code Example: Activity uses AsyncTask to get a value in a background thread, then AsyncTask returns the result back to the Activity by calling processValue:

public class MyClass extends Activity {
  private void getValue() {
      new MyTask().execute();
  }

  void processValue(Value myValue) {
     //handle value 
     //Update GUI, show toast, etc..
  }

  private class MyTask extends AsyncTask<Void, Void, Value> {
    @Override
    protected Value doInBackground(Void... params) {
      //do stuff and return the value you want 
      return Value;
    }

    @Override
    protected void onPostExecute(Value result) {
      // Call activity method with results
      processValue(result);
    }
  }
}

T
Tarsem Singh

you can try this one: myvalue = new myTask().execute().get(); minus is it will freeze process until asyncron will not be finished ;


any other way to avoide this freezing UI
Performing heavy tasks in the UI thread is discouraged and may lead to ANR. Moreover, performing a networking operation on main thread causes NetworkOnMainThreadException and immediate crash by design. Check that nice article: developer.android.com/training/articles/perf-anr#java
I
IgniteCoders

You need to use "protocols" to delegate or provide data to the AsynTask.

Delegates and Data Sources

A delegate is an object that acts on behalf of, or in coordination with, another object when that object encounters an event in a program. (Apple definition)

protocols are interfaces that define some methods to delegate some behaviors.

DELEGATE: Capturate events from the object in background thread

AsynkTask:

public final class TaskWithDelegate extends AsyncTask<..., ..., ...> {
    //declare a delegate with type of protocol declared in this task
    private TaskDelegate delegate;

    //here is the task protocol to can delegate on other object
    public interface TaskDelegate {
        //define you method headers to override
        void onTaskEndWithResult(int success);
        void onTaskFinishGettingData(Data result);
    }

    @Override
    protected Integer doInBackground(Object... params) {
        //do something in background and get result
        if (delegate != null) {
            //return result to activity
            delegate.onTaskFinishGettingData(result);
        }   
    }

    @Override
    protected void onPostExecute(Integer result) {
        if (delegate != null) {
            //return success or fail to activity
            delegate.onTaskEndWithResult(result);
        }   
    }
}

Activity:

public class DelegateActivity extends Activity implements TaskDelegate {
    void callTask () {
            TaskWithDelegate task = new TaskWithDelegate;
        //set the delegate of the task as this activity
        task.setDelegate(this);
    }

    //handle success or fail to show an alert...
    @Override
    void onTaskEndWithResult(int success) {

    }

    //handle data to show them in activity...
    @Override
    void onTaskFinishGettingData(Data result) {

    }
}

EDIT: if you call delegate in doInBackground, and the delegate try to edit some view, that will crash because view can be manipulated only by the main thread.

// this refers to Activity
this.runOnUiThread(new Runnable() {
    @Override
    public void run() {
    // Here you can edit views when task notify some changes from background thread
        textView.setText(someValue);
    }
});

EXTRA

DATASOURCE: Provide data to the object in background thread

AsyncTask:

public final class TaskWithDataSource extends AsyncTask<..., ..., ...> {
    //declare a datasource with type of protocol declared in this task
    private TaskDataSource dataSource;
    private Object data;

    //here is the task protocol to can provide data from other object
    public interface TaskDataSource {
        //define you method headers to override
        int indexOfObject(Object object);
        Object objectAtIndex(int index);
    }

    @Override
    protected void onPreExecute(Integer result) {
        if (dataSource != null) {
            //ask for some data
            this.data = dataSource.objectAtIndex(0);
        }   
    }

    @Override
    protected Integer doInBackground(Object... params) {
        //do something in background and get result
        int index;
        if (dataSource != null) {
            //ask for something more
            index = dataSource.indexOfObject(this.data);
        }   
    }
}

Activity:

public class DataSourceActivity extends Activity implements TaskDataSource {
    void callTask () {
            TaskWithDataSource task = new TaskWithDataSource;
        //set the datasource of the task as this activity
        task.setDataSource(this);
    }

    //send some data to the async task when it is needed...
    @Override
    Object objectAtIndex(int index) {
        return new Data(index);
    }

    //send more information...
    @Override
    int indexOfObject(Object object) {
        return new object.getIndex();
    }
}

So in other words, just use an interface and who ever needs to be informed would just register as a listener.
Yes, in few words that’s the idea.
I find this the cleanest answer. Task Class shouldn't know the implementation details of the Activity. Another important benefit is the Code Reuse, you can call this class for any other Activity/Fragments
N
Nikita Bosik

Use generic parameters

AsyncTask<Params, Progress, Result>

Params — task's input data type

Progress — how to inform the world about progress

Result — task's output data type

Think like

Result = task(Params)

Example

Load YourObject by string URL:

new AsyncTask<String, Void, YourObject>()
{
    @Override
    protected void onPreExecute()
    {
        /* Called before task execution; from UI thread, so you can access your widgets */

        // Optionally do some stuff like showing progress bar
    };

    @Override
    protected YourObject doInBackground(String... url)
    {
        /* Called from background thread, so you're NOT allowed to interact with UI */

        // Perform heavy task to get YourObject by string
        // Stay clear & functional, just convert input to output and return it
        YourObject result = callTheServer(url);
        return result;
    }

    @Override
    protected void onPostExecute(YourObject result)
    {
        /* Called once task is done; from UI thread, so you can access your widgets */

        // Process result as you like
    }
}.execute("http://www.example.com/");

S
Sunil Garg

For you get the result from a AsyncTask it needs to do it after the super.onPostExcecute(result); Because this means that the background and the AsyncTask also have finished. e.g.:

... into your async task

@Override
protected void onPostExecute(MyBeanResult result) {
    if (dialog.isShowing()) {
        dialog.dismiss();
    }
    if (mWakeLock.isHeld()) {
        mWakeLock.release();
    }
    super.onPostExecute(result);
    MyClassName.this.checkResponseForIntent(result);
}

and the methodr checkResponseForIntent might by something like this:

private void checkResponseForIntent(MyBeanResult response) {
    if (response == null || response.fault != null) {
        noServiceAvailable();
        return;
    }
 ... or do what ever you want, even call an other async task...

I had this problem using .get() from AsyncTask and the ProgressBar simply doesn't work with get(), actually, it only works once that doInBackground finishes.

I hope that it helps you.


thats true , the progress dialogue just wont work with get() method
H
Hamid

Pass the MainActivity to Async class, So you'll access the MainActivity functions in inner class, This works fine for me:

public class MainActivity extends ActionBarActivity {

   void callAsync()
   {
      Async as = new Async(this,12,"Test");
      as.execute();
   }

   public void ReturnThreadResult(YourObject result)
   {
     // TO DO:
     // print(result.toString());

   }
}


public class Async extends AsyncTask<String, String, Boolean> {
    MainActivity parent;
    int param1;
    String param2;

    public Async(MainActivity parent,int param1,String param2){
       this.parent = parent;
       this.param1 = param1;
       this.param2 = param2;
    }

    @Override
    protected void onPreExecute(){};

    @Override
    protected YourObject doInBackground(String... url)
    {
        return result;
    }

    @Override
    protected void onPostExecute(YourObject result)
    {
       // call an external function as a result 
       parent.ReturnThreadResult(result);
    }
  }