开发者

Stop method inside doInBackground of doAsyncTask

开发者 https://www.devze.com 2023-04-06 06:55 出处:网络
I have this AsyncTask: private class GetMyFlights extends AsyncTask<String, Void, Integer> { private ProgressDialog dialog;

I have this AsyncTask:

private class GetMyFlights extends AsyncTask<String, Void, Integer> {       
    private ProgressDialog dialog;  

    public GetMyFlights(ListActivity activity) {}           

    @Override
    protected Integer doInBackground(String... params) {
        return getData();
    }       

    @Override
    protected void onPostExecute(Integer result) {  
        super.onPostExecute(result);    
        //...
    }
}

When I change to another activity I want to stop it. So I set this:

@Override
protected void 开发者_如何学GoonPause() {
    if(mGetMyFlights != null){
        mGetMyFlights.cancel(true);
        Log.d(TAG, "MyFlights onPause, cancel task");
    }
    super.onPause();
}

But the code inside getData is still working when I change my activity. How can I be sure is stop?


From everything I've read, it seems that the cancel() method is not a reliable way to stop an AsyncTask. An interrupt is sent to the background thread, but this is only effective on interruptable tasks. The general consensus is that, to ensure the AsynTask is stopped, you should continually check isCancelled() within the doInBackground method of your AsyncTask.


What I usually do is call cancel(true) on the AsyncTask instance and check Thread.interrupted() in doInBackground. You could check isCancelled(), but that doesn't work if the actual work is done in some other class that is independent from your AsyncTask and doesn't know about it. For example (copied directly from my own getData() method, inside my Data class, independent from any activities, async tasks, etc.):

while ((line = reader.readLine()) != null) {
    if (Thread.interrupted()) throw new InterruptedException();
    content.append(line);
}

Just make sure to handle the InterruptedException in your doInBackground(), e.g.:

@Override
protected Integer doInBackground(String... params) {
    try {
        return getData();
    }
    catch (InterruptedException e) {
        Log.d("MyApp", "Girl, Interrupted");
        return -1;
    }
}

Also worth noting that onPostExecute() is not called if the task is cancelled. Instead, onCancelled() is called.

0

精彩评论

暂无评论...
验证码 换一张
取 消