我的应用程序需要下载一些大文件。我使用下载服务并在通知中显示进度。问题是在下载期间,用户可以从 3g 切换到 WIFI。在这种情况下,进度会停止,但不会引发异常。我该如何正确处理这种情况?
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();