在我的Android程序中,我有一些代码可以下载文件.这样工作正常,但是因为在手机上,你可以随时断开连接,我需要更改它重新连接并在你中途恢复下载并且有人打电话/你失去了小区接收/等等.我无法弄清楚如何检测InputStream已停止工作.请参阅以下代码:
InputStream in = c.getInputStream(); byte[] buffer = new byte[8024]; int len1 = 0; while ( (len1 = in.read(buffer)) > 0 ) { Log("-"+len1+"- Downloaded."); f.write(buffer,0, len1); Thread.sleep(50); }
当我丢失互联网连接时,我的日志显示:
Log: -8024- Downloaded. Log: -8024- Downloaded. Log: -8024- Downloaded. Log: -8024- Downloaded. Log: -6024- Downloaded. (some lower number)
然后我的程序暂时挂起((len1 =等我需要这样做当互联网断开时我等待互联网再次连接然后恢复下载.