How to completely kill/remove/delete/stop an AsyncTask

前端 未结 7 2099
终归单人心
终归单人心 2020-11-28 09:57

I made an app that downloads videos from our server. The issue is:

When i cancel the downloading i call:

myAsyncTask.cancel(true)

I

7条回答
  •  [愿得一人]
    2020-11-28 10:36

    I have been researching from the last 2 weeks and I don't get to know that how we kill the Async operation manually. Some developers use BREAK; while checking in for loop. But on my scenario I am not using the loop inside of background thread. But I have got to know how it woks its a stupid logic but works perfectly fine.

    downloadFile.cancel(true);   //This code wont work in any case.
    

    Instead of canceling and doing so much work on background thread turn off the wifi programmatically

    WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    wifi.setWifiEnabled(false);
    

    where do you want to kill the operation and turn on it where do you need that.

    WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    wifi.setWifiEnabled(true);
    

    What happens is your try block jumps in to the IOException killing the background tasks.

提交回复
热议问题