0
public class Calculate extends AsyncTask<Void, Integer, Integer> {

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

    @Override
    protected Integer doInBackground(Void... arg0) {
        int a = 1;
                int b = 2

        return a+b;
    }

    @Override
    protected void onPostExecute(Integer result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

    }

}

my app use asynctask class (below), and a function to call this class, my question is how to know when this class is finished ? every time i check, its always Running !

Ahmad
  • 69,608
  • 17
  • 111
  • 137
Hiếu Giề
  • 97
  • 1
  • 16
  • This code won't even compile.. – Kon Feb 08 '14 at 17:07
  • @Kon can you explain why this won't compile? – Shayan Pourvatan Feb 08 '14 at 17:08
  • 1
    Missing semicolon in doInBackground method. – Kon Feb 08 '14 at 17:09
  • `onPostExecute` is last method to run in `AsynkTask` class, so last line of `onPostExecute` is last line in this class – Shayan Pourvatan Feb 08 '14 at 17:10
  • If this is a separate file from the `Activity` you can use [an interface](http://stackoverflow.com/questions/18517400/inner-class-can-access-but-not-update-values-asynctask/18517648#18517648) to update the `Activity` with a callback to do whatever you want. If it is an inner-class then you can call any method you need in `onPostExecute()` which will be called when `doInBackground()` finishes. – codeMagic Feb 08 '14 at 17:12
  • Also, second `param` of declaration (`AsyncTask`) is what `onProgressUpdate()` takes so this should be `Void` if you aren't calling `publishProgress()` in `doInBackground()` – codeMagic Feb 08 '14 at 17:14

2 Answers2

1

In your onPostExecute(...) method, you could simply call a method in your caller Activity which would set a boolean terminated_activity to true. You can do this in several ways, most probably the easiest ones are Intents combined with a Handler, or a local BroadcastReceiver.

  • An example on Handlers and Intents is in an answer I posted today in other question, here.
  • A nice explaination on local BroadcastReceivers is here.
Community
  • 1
  • 1
nKn
  • 13,691
  • 9
  • 45
  • 62
0

onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.

Directly from the docs

DieselPower
  • 738
  • 2
  • 6
  • 22