0 .Thanks
Android系統(tǒng)下載管理DownloadManager功能介紹及使用示例
1 .概述
DowanloadManger:系統(tǒng)的服務(wù),系統(tǒng)開放給第三方應(yīng)用使用的類库北,用于管理下載威沫。
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.
可以理解為孵构,允許媒體的掃描颈墅。需要在任務(wù)開始前調(diào)用雾袱。
-
setAllowedNetworkTypes(int flags)
Restrict the types of networks over which this download may proceed.
限制什么類型的網(wǎng)絡(luò)可以下載:-
DownloadManager.Request.NETWORK_MOBILE
-Wifi或者移動網(wǎng)絡(luò)可以下載 -
DownloadManager.Request.NETWORK_WIFI
-只允許Wifi的環(huán)境下芹橡,下載。
-
setAllowedOverMetered(boolean allow)
計量網(wǎng)絡(luò)下載煎殷?不懂腿箩。默認允許。setAllowedOverRoaming(boolean allowed)
允許漫游網(wǎng)絡(luò)弓乙?默認運行钧惧。setDescription(CharSequence description)
Set a description of this download, to be displayed in notifications (if enabled)
設(shè)置下載的描述浓瞪,會顯示在通知欄,如果允許的話追逮。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).
設(shè)置文件的存放目錄粹舵。這個方法設(shè)置的是眼滤,應(yīng)用程序內(nèi)能訪問的历涝。其可以用的路徑: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)).
設(shè)置文件的存放目錄荧库。這個方法設(shè)置的是,程序的外部存儲场刑。其可以用的路徑:Environment.getExternalFilesDir(String).
或者蚪战,可以自定義。setDestinationUri(Uri uri)
Set the local destination for the downloaded file.
設(shè)置下載地址瞎疼。為uri.setMimeType(String mimeType)
Set the MIME content type of this download.
設(shè)置下載類型: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.
設(shè)置通知欄是否顯示和顯示的規(guī)則:- VISIBILITY_HIDDEN :不顯示在通知欄
- VISIBILITY_VISIBLE :顯示在通知欄贼急,下載完成后捏萍,消失照弥。
- VISIBILITY_VISIBLE_NOTIFY_COMPLETED :顯示在通知欄这揣,下載完成后,也顯示给赞。
- VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION :顯示在通知欄片迅,只是完成后顯示柑蛇。下載的時候,不顯示耻台。
setRequiresCharging(boolean requiresCharging)
需要插入才下載?不懂setRequiresDeviceIdle(boolean requiresDeviceIdle)
空閑的時候才下載蹋砚?setTitle(CharSequence title)
設(shè)置下載題目坝咐,顯示在通知欄析恢,如果可以的話。setVisibleInDownloadsUi(boolean isVisible)
Set whether this download should be displayed in the system's Downloads UI.
設(shè)置下載的地址URI是否能在通知欄上可見框杜。
3 .DownloadManager.Query
查詢?nèi)蝿?wù)袖肥。
setFilterById(long... ids)
Include only the downloads with the given IDs.
根據(jù)下載的ID返回任務(wù):DownloadManager.QuerysetFilterByStatus(int flags)
include only downloads with status matching any the given status flags.
根據(jù)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:
}
}
}
去查詢?nèi)蝿?wù)的狀態(tài)咪辱。
下載的監(jiān)聽:
/**
* 得到下載的狀態(tài)
* @param context 上下文
* @param downloadId ID
* @return 0 : 已經(jīng)下載的量(bytes) , 1 : 總的大小, 2 : 下載的狀態(tài)
*/
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;
}