简体   繁体   中英

how to know when asynctask is finished

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 !

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 Handler s and Intent s is in an answer I posted today in other question, here .
  • A nice explaination on local BroadcastReceiver s is here .

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

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