一。downloadmanager類說明:
從Android 2.3開始新增瞭一個下載管理類,在SDK的文檔中我們查找android.app.DownloadManager可以看到。下載管理類可以長期處理多個HTTP下載任務,客戶端隻需要給出請求的Uri和存放目標文件的位置即可,下載管理使用瞭一個AIDL服務器所以可以放心的在後臺執行,同時實例化的方法需要使用getSystemService(Context.DOWNLOAD_SERVICE) ,Android123再次提醒使用API Level為9的用戶可以輕松的通過新增的這個API實現Android平臺上的文件下載操作。
DownloadManager類提供瞭以下幾種方法來處理,
Java代碼 long enqueue(DownloadManager.Request request) //存入隊列一個新的下載項
ParcelFileDescriptor openDownloadedFile(long id) //打開一個下載後的文件用於讀取,參數中的long型id是一個provider中的一條記錄。
Cursor query(DownloadManager.Query query) //查詢一個下載,返回一個Cursor
int remove(long… ids) //取消下載同時移除這些條從下載管理中。
我們可以看到提供的方法都比較簡單,給我們操作的最終封裝成為一個provider數據庫的方式進行添加、查詢和移除,但是對於查詢和添加任務的細節,我們要看看DownloadManager.Request類和DownloadManager.Query 類瞭。
一、DownloadManager.Request類的成員和定義
Java代碼 DownloadManager.Request addRequestHeader(String header, String value) // 添加一個Http請求報頭,對於這兩個參數,Android開發網給大傢舉個小例子,比如說User-Agent值可以為Android123或 Windows XP等等瞭,主要是給服務器提供標識。
DownloadManager.Request setAllowedNetworkTypes(int flags) //設置允許使用的網絡類型,這一步Android 2.3做的很好,目前有兩種定義分別為NETWORK_MOBILE和NETWORK_WIFI我們可以選擇使用移動網絡或Wifi方式來下載。
DownloadManager.Request setAllowedOverRoaming(boolean allowed) //對於下載,考慮到流量費用,這裡是否允許使用漫遊。
DownloadManager.Request setDescription(CharSequence description) //設置一個描述信息,主要是最終顯示的notification提示,可以隨便寫個自己區別
DownloadManager.Request setDestinationInExternalFilesDir(Context context, String dirType, String subPath) //設置目標存儲在外部目錄,一般位置可以用 getExternalFilesDir()方法獲取。
DownloadManager.Request setDestinationInExternalPublicDir(String dirType, String subPath) //設置外部存儲的公共目錄,一般通過getExternalStoragePublicDirectory()方法獲取。
DownloadManager.Request setDestinationUri(Uri uri) //設置需要下載目標的Uri,可以是http、ftp等等瞭。
DownloadManager.Request setMimeType(String mimeType) //設置mime類型,這裡看服務器配置,一般國傢化的都為utf-8編碼。
DownloadManager.Request setShowRunningNotification(boolean show) //是否顯示下載進度的提示
DownloadManager.Request setTitle(CharSequence title) //設置notification的標題
DownloadManager.Request setVisibleInDownloadsUi(boolean isVisible) //設置下載管理類在處理過程中的界面是否顯示
當然瞭Google還提供瞭一個簡單的方法來實例化本類,這個構造方法為DownloadManager.Request(Uri uri) ,我們直接填寫一個Uri即可,上面的設置使用默認情況。
二、DownloadManager.Query類
對於當前下載內容的狀態,我們可以使用DownloadManager.Query類來獲取,本類比較簡單,僅僅提供瞭兩個方法。
Java代碼 DownloadManager.Query setFilterById(long… ids) //根據id來過濾查找。
DownloadManager.Query setFilterByStatus(int flags) //根據任務的狀態來查找。
詳細的狀態在android.app.DownloadManager類中有定義,目前Android 2.3中的定義為:
Java代碼 int STATUS_FAILED 失敗
int STATUS_PAUSED 暫停
int STATUS_PENDING 等待將開始
int STATUS_RUNNING 正在處理中
int STATUS_SUCCESSFUL 已經下載成功
最後Android開發網提醒大傢要說的是因為DownloadManager類提供的query方法返回一個Cursor對象,這些狀態保存在這個遊標的COLUMN_STATUS 字段中。
1. 下載的狀態完成均是以廣播的形式通知大傢,目前API Level為9定義瞭下面三種Intent的action
Java代碼 ACTION_DOWNLOAD_COMPLETE下載完成的動作。
ACTION_NOTIFICATION_CLICKED 當用戶單擊notification中下載管理的某項時觸發。
ACTION_VIEW_DOWNLOADS 查看下載項
2. 對於一個尚未完成的項,在Cursor中我們查找COLUMN_REASON字段,可能有以下定義:
Java代碼 int ERROR_CANNOT_RESUME 不能夠繼續,由於一些其他原因。
int ERROR_DEVICE_NOT_FOUND 外部存儲設備沒有找到,比如SD卡沒有插入。
int ERROR_FILE_ALREADY_EXISTS 要下載的文件已經存在瞭,Android123提示下載管理類是不會覆蓋已經存在的文件,所以如果需要重新下載,請先刪除以前的文件。
int ERROR_FILE_ERROR 可能由於SD卡原因導致瞭文件錯誤。
int ERROR_HTTP_DATA_ERROR 在Http傳輸過程中出現瞭問題。
int ERROR_INSUFFICIENT_SPACE 由於SD卡空間不足造成的
int ERROR_TOO_MANY_REDIRECTS 這個Http有太多的重定向,導致無法正常下載
int ERROR_UNHANDLED_HTTP_CODE 無法獲取http出錯的原因,比如說遠程服務器沒有響應。
int ERROR_UNKNOWN 未知的錯誤類型.
3. 有關暫停的一些狀態,同樣COLUMN_REASON字段的值可能是以下定義
Java代碼 int PAUSED_QUEUED_FOR_WIFI 由於移動網絡數據問題,等待WiFi連接能用後再重新進入下載隊列。
int PAUSED_UNKNOWN 未知原因導致瞭任務下載的暫停.
int PAUSED_WAITING_FOR_NETWORK 可能由於沒有網絡連接而無法下載,等待有可用的網絡連接恢復。.
int PAUSED_WAITING_TO_RETRY 由於重重原因導致下載暫停,等待重試。
有關Android 2.3中新增的下載管理DownloadManager的介紹基本上已經完全講完,如果你對Cursor、Provider這些基礎概念瞭解的話,可以看到這個下載管理類可以幫我們減少很多不必要的代碼編寫
二。用downloadManager 下載:
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype, long contentLength) {
AppUtils.LogD(mimetype);
// download file
DownloadManager downloadManager = ((DownloadManager) activity
.getSystemService(Activity.DOWNLOAD_SERVICE));
Request request = new Request(Uri.parse(url));
// set request header, 如session等 www.aiwalls.com
request.addRequestHeader("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
request.addRequestHeader("Accept-Language", "en-us,en;q=0.5");
request.addRequestHeader("Accept-Encoding", "gzip, deflate");
request.addRequestHeader("Accept-Charset",
"ISO-8859-1,utf-8;q=0.7,*;q=0.7");
request.addRequestHeader("Cache-Control", "max-age=0");
String host = "";
try {
host = new URL(url).getHost();
} catch (MalformedURLException e) {
e.printStackTrace();
}
String cookieStr = CookieManager.getInstance().getCookie(host);
if (!AppUtils.isEmpty(cookieStr)) {
request.addRequestHeader("Cookie", cookieStr + "; AcSe=0");
}
// generate filename dynamically
String fileName = contentDisposition.replaceFirst(
"attachment; filename=", "");
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS, fileName);
downloadManager.enqueue(request);
}
三。判斷download finished:
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
long downloadId = intent.getLongExtra(
DownloadManager.EXTRA_DOWNLOAD_ID, 0);
Query query = new Query();
query.setFilterById(enqueue);
Cursor c = dm.query(query);
if (c.moveToFirst()) {
int columnIndex = c
.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c
.getInt(columnIndex)) {
ImageView view = (ImageView) findViewById(R.id.imageView1);
String uriString = c
.getString(c
.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
view.setImageURI(Uri.parse(uriString));
}
}
}
}
};
registerReceiver(receiver, new IntentFilter(
DownloadManager.ACTION_DOWNLOAD_COMPLETE));
四。獲取系統download history list:
Intent i = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
startActivity(i);
五。 獲得剛下載的文件 和 打開該文件:
protected String getDownloadedFileName() {
String fileName = "";
DownloadManager downloadManager = (DownloadManager) activity
.getSystemService(Activity.DOWNLOAD_SERVICE);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterByStatus(DownloadManager.STATUS_SUCCESSFUL);
Cursor c = downloadManager.query(query);
if (c.moveToFirst()) {
fileName = c.getString(c
.getColumnIndex(DownloadManager.COLUMN_TITLE));
}
return fileName;
}
public void openFileFromSDCard(String fileStr, String mimeType) {
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(fileStr);
intent.setDataAndType(Uri.fromFile(file), mimeType);
activity.startActivity(intent);
}
摘自 fhy_2008的專欄