Skip to content Skip to sidebar Skip to footer

How To Call A Returning Value Function In Asynctask

I am new to andorid programming I've just stucked into a problem to call returning value function inside AsyncTask's doInBackground() Simple question is How to wait for AsyncTask t

Solution 1:

For myself i do this with a callback Function, that i invoke after onPostExecute.

public AsyncUnzip(Activity ctx, Observer callback) {
    this.ctx = ctx;
    this.callback = callback;
}

and

@OverrideprotectedvoidonPreExecute() {
    super.onPreExecute();
    dia = newProgressDialog(ctx);
    dia.setTitle("Bitte warten");
    dia.setMessage("Geodatenpaket wird entpackt...");
    dia.setCancelable(false);
    dia.show();
}

and

@OverridepublicvoidonPostExecute(Boolean result ) {
    super.onPostExecute(result);
    dia.dismiss();
    callback.update(null, returnFolder);
    System.out.println("Unzipped to: " + returnFolder.getName() );
}

In that case your call of Async Task would look like that:

AsyncUnzipunzipThread=newAsyncUnzip(ImportActivity.this, newObserver() {
    @Overridepublicvoidupdate( Observable observable, Object data ) {//your code invoked after Async Task
} });
unzipThread.execute(selectedFile); //Start Unzip in external Thread.

Note: This is a quick and diry solution with a annonymous Observer implementation and without an observable.

Solution 2:

You can create an Interface and return value from onPostExecute() of AsyncTask or you can register a BroadcastReceiver and fire in from onPostExecute() method of AsyncTask. I had created a demo using Interface and BroadcastReceiver you can download and check it.

Post a Comment for "How To Call A Returning Value Function In Asynctask"