0 .Thanks
Android系統下載管理DownloadManager功能介紹及使用示例
1 .概述
DowanloadManger:系統的服務,系統開放給第三方應用使用的類,用于管理下載。
DownloadManager.Query:用來查詢下載信息
DownloadManager.Request:用來請求一個下載
2 .DownloadManager.Request
addRequestHeader(String header, String value)
Add an HTTP header to be included with the download request.
添加一個Http的頭到下載的請求。
allowScanningByMediaScanner()
If the file to be downloaded is to be scanned by MediaScanner, this method should be called before enqueue(Request) is called.
可以理解為,允許媒體的掃描。需要在任務開始前調用。
-
setAllowedNetworkTypes(int flags)
Restrict the types of networks over which this download may proceed.
限制什么類型的網絡可以下載:-
DownloadManager.Request.NETWORK_MOBILE
-Wifi或者移動網絡可以下載 -
DownloadManager.Request.NETWORK_WIFI
-只允許Wifi的環境下,下載。
-
setAllowedOverMetered(boolean allow)
計量網絡下載?不懂。默認允許。setAllowedOverRoaming(boolean allowed)
允許漫游網絡?默認運行。setDescription(CharSequence description)
Set a description of this download, to be displayed in notifications (if enabled)
設置下載的描述,會顯示在通知欄,如果允許的話。setDestinationInExternalFilesDir(Context context, String dirType, String subPath)
Set the local destination for the downloaded file to a path within the application's external files directory (as returned by getExternalFilesDir(String).
設置文件的存放目錄。這個方法設置的是,應用程序內能訪問的。其可以用的路徑:context.getExternalFilesDir(String).setDestinationInExternalPublicDir(String dirType, String subPath)
Set the local destination for the downloaded file to a path within the public external storage directory (as returned by getExternalStoragePublicDirectory(String)).
設置文件的存放目錄。這個方法設置的是,程序的外部存儲。其可以用的路徑:Environment.getExternalFilesDir(String).
或者,可以自定義。setDestinationUri(Uri uri)
Set the local destination for the downloaded file.
設置下載地址。為uri.setMimeType(String mimeType)
Set the MIME content type of this download.
設置下載類型:MIME 參考手冊
apk的Mime:application/vnd.android-
setNotificationVisibility(int visibility)
Control whether a system notification is posted by the download manager while this download is running or when it is completed.
設置通知欄是否顯示和顯示的規則:- VISIBILITY_HIDDEN :不顯示在通知欄
- VISIBILITY_VISIBLE :顯示在通知欄,下載完成后,消失。
- VISIBILITY_VISIBLE_NOTIFY_COMPLETED :顯示在通知欄,下載完成后,也顯示。
- VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION :顯示在通知欄,只是完成后顯示。下載的時候,不顯示。
setRequiresCharging(boolean requiresCharging)
需要插入才下載?不懂setRequiresDeviceIdle(boolean requiresDeviceIdle)
空閑的時候才下載?setTitle(CharSequence title)
設置下載題目,顯示在通知欄,如果可以的話。setVisibleInDownloadsUi(boolean isVisible)
Set whether this download should be displayed in the system's Downloads UI.
設置下載的地址URI是否能在通知欄上可見。
3 .DownloadManager.Query
查詢任務。
setFilterById(long... ids)
Include only the downloads with the given IDs.
根據下載的ID返回任務:DownloadManager.QuerysetFilterByStatus(int flags)
include only downloads with status matching any the given status flags.
根據Flags去過濾返回:DownloadManager.Query
查詢可以通過:
private void queryDownloadStatus() {
DownloadManager.Query query = new DownloadManager.Query();
final DownloadManager downloadManager= (DownloadManager)
this.getSystemService(Context.DOWNLOAD_SERVICE);
query.setFilterById(TaskID);
Cursor c = downloadManager.query(query);
if(c.moveToFirst()) {
int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
switch(status) {
case DownloadManager.STATUS_PAUSED:
case DownloadManager.STATUS_PENDING:
case DownloadManager.STATUS_RUNNING:
case DownloadManager.STATUS_SUCCESSFUL:
}
}
}
去查詢任務的狀態。
下載的監聽:
/**
* 得到下載的狀態
* @param context 上下文
* @param downloadId ID
* @return 0 : 已經下載的量(bytes) , 1 : 總的大小, 2 : 下載的狀態
*/
public static int[] getBytesAndStatus(Context context, long downloadId) {
int[] bytesAndStatus = new int[] { -1, -1, -1 };
DownloadManager.Query query = new DownloadManager.Query()
.setFilterById(downloadId);
DownloadManager downloadManager = (DownloadManager)
context.getSystemService(Context.DOWNLOAD_SERVICE);
Cursor c = null;
try {
c = downloadManager.query(query);
if (c != null && c.moveToFirst()) {
bytesAndStatus[0] = c.getInt(c.getColumnIndexOrThrow(DownloadManager
.COLUMN_BYTES_DOWNLOADED_SO_FAR));
bytesAndStatus[1] = c.getInt(c.getColumnIndexOrThrow(DownloadManager
.COLUMN_TOTAL_SIZE_BYTES));
bytesAndStatus[2] = c.getInt(c.getColumnIndex(DownloadManager
.COLUMN_STATUS));
}
} finally {
if (c != null) {
c.close();
}
}
return bytesAndStatus;
}