[英]DownloadManager vs InputStream.read()
I have 2 simple implementation of DownloadManager
and custom download task: 我有2个简单的
DownloadManager
实现和自定义下载任务:
DownloadManager
public void onCreate(Bundle savedInstanceState) { ... mDownloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); } private void startDownload(String link) Uri uri=Uri.parse(link); DownloadManager.Request req = new DownloadManager.Request(uri); req.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE) .setAllowedOverRoaming(false) .setVisibleInDownloadsUi(false) .setTitle(getString(R.string.app_name)) .setDescription(getString(R.string.waiting_for_downloading)) .setDestinationInExternalPublicDir(dataPath,fileName); lastDownload = mDownloadManager.enqueue(req); } private void queryStatus() { Cursor c = mDownloadManager.query(new DownloadManager.Query().setFilterById(lastDownload)); if (c == null) return; switch (c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS))) { case DownloadManager.STATUS_FAILED: downloadError(); break; case DownloadManager.STATUS_RUNNING: ... break; case DownloadManager.STATUS_SUCCESSFUL: ... break; default: break; } c.close(); }
custom download task 自定义下载任务
new AsyncTask<String, Void, Void>() { @Override protected Void doInBackground(String... params) { try { URL url = new URL(params[0]); InputStream input = new BufferedInputStream(url.openStream()); OutputStream output = new FileOutputStream(saveFile); byte data[] = new byte[1024]; while ((count = input.read(data)) != -1) { output.write(data, 0, count); } output.flush(); output.close(); input.close(); } catch (Exception e) { downloadError(); } return null; } }
in general cases both methods works ok, but on some devices (for ex xiaomi android 6) DownloadManager
works 2-3 times slower or i got DownloadManager.STATUS_FAILED
error. 在一般情况下,两种方法都可以,但是在某些设备上(例如适用于小米android 6),
DownloadManager
运行速度要慢2-3倍,或者出现DownloadManager.STATUS_FAILED
错误。 but why? 但为什么? is
DownloadManager
unstable? DownloadManager
不稳定吗? or it's smth about settings? 还是关于设置的秘密?
ps: i would like to use DownloadManager
because of customization that i shouldn't implement by myself like set up allowed network or show progress in notification area. ps:我想使用
DownloadManager
因为我不应该自己执行自定义,例如设置允许的网络或在通知区域显示进度。
update v0.1 更新v0.1
1) I have handler that called queryStatus()
check one time per 1 sec. 1)我有调用
queryStatus()
处理程序,每1秒检查一次。
2) trying to connect to another wi-fi
network with nexus phone - DownloadManager
works ok, but custom implementation fails... 2)尝试连接到其他
wi-fi
网络的Nexus手机- DownloadManager
工作正常,但自定义实现失败...
update v0.2 更新v0.2
tried to call AsyncTask
downloader from Service
(to continue download in case user close the app without canceling process) , UI updates via BroadcastReceiver
+ sendBreadcase()
call from the Service
- result: downloading became several times slower and now (from time to time) I got an unexpected end of stream
exception. 尝试从
Service
调用AsyncTask
下载器(以在用户关闭应用而未取消进程的情况下继续下载) ,通过Service
BroadcastReceiver
+ sendBreadcase()
UI更新-结果:下载变得慢了几倍,而现在(时不时) unexpected end of stream
异常异常unexpected end of stream
。 [Trying the same files I've downloaded when makes call from Activity
] [尝试从
Activity
拨打电话时下载的相同文件]
update v0.3 更新v0.3
forgot to notice: DownloadManager
fails with 1008
reason = ERROR_CANNOT_RESUME
忘了注意:
DownloadManager
失败,原因为1008
原因= ERROR_CANNOT_RESUME
tried ION also. 也尝试过ION 。 looks if works better than
DownloadManager
but some times got an error End of data reached before content length was read: 70385664/77217546
看起来是否比
DownloadManager
更好,但有时会出现错误End of data reached before content length was read: 70385664/77217546
so, still looking the most stable solution... 因此,仍在寻找最稳定的解决方案...
ok... there was a lot of tests passed with different wi-fi connections and set of devices. 好的...通过不同的Wi-Fi连接和设备集通过了许多测试。
ION
grant the most stable results. ION
提供最稳定的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.