android自带的DownloadManager如何实现断点续传下载功能?
is it possible to get somehow this http code?
Currently, no. The DownloadManager reports a STATUS_SUCCESSFUL even when a download failed, for example because the url/file was not found (HTTP 404) (this is a bug).
Also see DownloadManager sends STATUS_SUCCESSFUL for failed download
I know it's a relatively old thread but that issue still remains. I tested it 5 minutes ago and it still doesn't work properly.
is it possible to listen to "download pause" event without querying constantly the download manager?
Weirdly, no. The only available "events" to listen for are: enter image description here
To get around this, you have to query the status yourself every X time by checking
if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_PAUSED) {
// Do stuff
}
is download manager (on API level 16+) supports https (ssl) ?
It used to not support https (read more in Android DownloadManager and SSL (https)), however it does now. You can simply verify by trying to retrieve a file from a https origin, for example https://mdn.mozillademos.org/files/3794/mixed_content_webconsole.jpg. You will see that it retrieves the file fine.
what exactly is download manager retry policy? can I change it default retry policy?
It is currently not possible to change the retry 'policy'. See the docs and you will find there are no methods or properties regarding this functionality.
Regarding the default retry policy, useful information can be found in the following package: com.android.providers.downloads.Constants. This links to the 5.1.1 version, if you need the information for another version you can manually navigate to that. For example here is the information for android 4.0.1 (the retry policy values are the same as in 5.1.1).
It states:
The number of times that the download manager will retry its network operations when no progress is happening before it gives up.
public static final int MAX_RETRIES = 5;
The minimum amount of time that the download manager accepts for a Retry-After response header with a parameter in delta-seconds.
public static final int MIN_RETRY_AFTER = 30; // 30s
The maximum amount of time that the download manager accepts for a Retry-After response header with a parameter in delta-seconds.
public static final int MAX_RETRY_AFTER = 24 * 60 * 60; // 24h
As you might have guessed, these are final (constant) and thus cannot be changed.
Conclusion: DownloadManager is pretty useful to do some basic downloading, but it's functionality is pretty limited.
I can suggest an alternative: there's a downloadmanager in the android-common libary over at https://github.com/Trinea/android-common
很不幸地告诉你,DownloadManager几乎无法实现这个功能。
官方建议使用这个库 https://github.com/Trinea/android-common
硬性解:
private boolean resumeDownload(Context context, String downloadTitle) {
int updatedRows = 0;
ContentValues resumeDownload = new ContentValues();
resumeDownload.put("control", 0); // Resume Control Value
try {
updatedRows = context
.getContentResolver()
.update(Uri.parse("content://downloads/my_downloads"),
resumeDownload,
"title=?",
new String[]{ downloadTitle });
} catch (Exception e) {
Log.e(TAG, "Failed to update control for downloading video");
}
return 0 < updatedRows;
}
private boolean pauseDownload(Context context, String downloadTitle) {
int updatedRows = 0;
ContentValues pauseDownload = new ContentValues();
pauseDownload.put("control", 1); // Pause Control Value
try {
updatedRows = context
.getContentResolver()
.update(Uri.parse("content://downloads/my_downloads"),
pauseDownload,
"title=?",
new String[]{ downloadTitle });
} catch (Exception e) {
Log.e(TAG, "Failed to update control for downloading video");
}
return 0 < updatedRows;
}
这个主要看你的服务器是否支持分块传输,也就是是否支持Range & Content-Range这两个请求头
如果支持,那么客户端就可以实现了,首先根据connentlength创建一个相同大小的本地文件,再创建一个位图文件(也就是以某个大小为单位,写入一系列0,表示这一块没有传输过)
下载的时候,每次从位图中取得某个0(没有下载)的块的位置,请求range块,返回数据,写入下载文件的指定位置,并且在位图上把这一块对应的0设置为1。
次与次中间,可以断开程序。
什么时候位图全部都是1了,就是下载成功。