开发者

Cancel AsyncTask

Is it possible to cancel AsyncTask that has one line command in doInBackground that performs a 开发者_StackOverflow中文版very long operation,like

@Override
protected Boolean doInBackground(String... filename) {
    fetchfile(filename[0]);
    // ...
    // ...
    return ...;
}


You can AsyncTask.cancel() to cancel. You can cancel it inside the fetchFile method at an appropriate place where you feel it will not introduce inconsistencies in your data or at a place where its easier to rollback.


In your long run method, periodically check isCanceled, if it's true, return from function ASAP so that async thread may be closed. And you may call cancel() from any thread.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜