简体   繁体   中英

get result from async task in not activity class

I use async task to get data from my database. i have :

public class BackgroundDatabaseTask extends AsyncTask<String, Void, String> {
    String jsonData;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... values) {
        String jsonData = Driver.returnJsonDataFromDatabase(values[0]);
        return jsonData;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(String result) {
        jsonData = result;
    }
}

And in other class i use it like:

private static String returnJsonDataBackgroundTaskExecute(String fromWhichTableGetData) {

    try {
        return new BackgroundDatabaseTask().execute(fromWhichTableGetData).get();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }

    return "Error in BackgroundDatabaseTask";
}

But get() block my main thread. So, how can I get result of my async task in other non activity class? I want run this in not activity class, so my class don't have onCreate method, but I have activity from my MainActivity class.

UPDATE: Now i solve this problem using thread but it is a good solution?

Runnable runnable = new Runnable() {
        @Override
        public void run() {
            listOfDataFromDatabase = GetterDataFromDatabase.returnJsonDataBackgroundTaskExecute(tableNameFromWhichIGetData);
        }
    };
    Thread thread = new Thread(runnable);
    thread.start();
    try {
        thread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

now i have acces to this varaible listOfDataFromDatabase in other method .

An AsyncTask creates a new thread to perform a task that takes a long time. You start this thread by calling execute() . However, you immediately call get() which waits for the task to finish. This completely destroys the whole point of using an AsyncTask in the first place.

Instead, you should remove the call to get() and do the final processing in onPostExecute() . You can do whatever you wish in this method. There is absolutely no requirement that you use the Activity in any way. You can provide data to other classes if that is what you wish.

You can define an interface in your Asynctask class then implement it where ever you want and get the result from that interface callback

MyTask extends AsynTask{
public interface DataListener{
void onDataReceived(String result);
}
/// then on your onPostExecute method , get an instance of the interface then push the result to the interface method 
dataListener.onDataReceived(result);
}

maybe this will help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM