I'm making an app with a download process, it will download a certain file. If I turn off the Wi-Fi when the file is downloading, the app crashes.

This is the log: recvfrom failed: ETIMEDOUT (Connection timed out)

I have a conditional, but it seems not to work. If I debug the code, it seems to enter the conditional.

```
else {
    Thread.sleep(4000); //doesn't work, doesn't sleep
    downloadresult = false;
}

```

I want the download process to pause when I turn off the Wi-Fi. Is there any way of doing this? 
Thanks in advance.

**All code**

```
 protected String doInBackground(String... f_url) {
        try {
            long total = 0;
            URL url = new URL(f_url[0]);
            HttpURLConnection conection = (HttpURLConnection) url.openConnection();
            int lenghtOfFile = conection.getContentLength();
            BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(file));
            conection.connect();
            BufferedInputStream input = new BufferedInputStream(conection.getInputStream());

            byte data[] = new byte[8192];
            int lastcount = 0;

            while ((count = input.read(data)) != -1) {

                if (isCanceled) { // this code waiting the click button :)
                    file.delete();
                    downloadresult = false;
                    break;
                }
                if (intCheck()) { // check internet and download
                    total += count;
                    downloadresult = true;
                    int ProgBarCount = (int) ((total * 100) / lenghtOfFile);
                    if (ProgBarCount > lastcount) {
                        lastcount = ProgBarCount;
                        publishProgress(Integer.toString(ProgBarCount));
                    }
                    output.write(data, 0, count);

                }
                else {

                     Thread.sleep(4000); //doesn't work, doesn't sleep
                     downloadresult = false;
                }

            }

            output.flush();
            output.close();
            input.close();
        }
        catch (Exception e) {

            e.printStackTrace();
         exmessage = e.getMessage().toString();
            downloadresult = false;

        }
        return null;
    }

```

If I debug the code, it works perfectly. If the app can't dowload the file, I want the app to wait 4000ms and then try again, but if I run the app, it crashes.

How can i pause/resume the download process. Thank You