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.
精彩评论