How to handle switch from 3G to WIFI? UrlConnection TimeOut does not throw Exception
-
10-10-2019 - |
Question
My application needs to download some big Files. I use a service for the download an show the progress in a notification. The problem is that during that download time the user can switch from 3g to WIFI. In that case the progress stops but no exception is thrown. How do I handle this situation properly?
URL url = new URL(myurl);
URLConnection conexion = url.openConnection();
conexion.setReadTimeout(10000);
conexion.setConnectTimeout(10000);
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
// downlod the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(targetFilePath);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
updateProgress(downloadNM, downloadNotification, (int)total*100/lenghtOfFile);
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
Solution
Take a look at the HttpClient-Library. It provides much more options than the URL class, and might help you with your problem.
http://developer.android.com/reference/org/apache/http/client/HttpClient.html
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow