AsyncTask is not cancelling the long running operation in android

寵の児 提交于 2019-12-05 14:47:58

I agree with Ran - you didn't write the code of: myUtilObject.convertInputStreamToString but im guessing you loop there over the inputstream with a buffer you predefined its size (maybe with BufferedReader?) - in this loop you should check the stop condition of your async thread - isCancelled() is a good example.

The loop should stop if the thread is canceled, i.e.:

String line = "";
StringBuilder total = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is), 1024);
while ((line = rd.readLine()) != null && !isCancelled())
{
     total.append(line);
}

You are checking the isCancelled() method after you perform the download, so there's no reason it would get cancelled in the middle.

Usually you do something like this:

while (!isCancelled()) {
 // Download next buffer..
}

In this case, the loop will stop once a cancel request has been made.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!