2

私のアプリケーションは、いくつかの大きなファイルをダウンロードする必要があります。ダウンロードにサービスを使用し、通知で進行状況を表示します。問題は、そのダウンロード時間中に、ユーザーが 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();
4

1 に答える 1

1

HttpClient-Library を見てください。URL クラスよりもはるかに多くのオプションを提供し、問題の解決に役立つ可能性があります。

http://developer.android.com/reference/org/apache/http/client/HttpClient.html

于 2010-11-16T10:16:01.027 に答える