Android基于自帶的DownloadManager實(shí)現(xiàn)下載功能示例
本文實(shí)例講述了Android基于自帶的DownloadManager實(shí)現(xiàn)下載功能。分享給大家供大家參考,具體如下:
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(APK_URL));
request.setDestinationInExternalPublicDir(DOWNLOAD_FOLDER_NAME, DOWNLOAD_FILE_NAME);
request.setTitle(getString(R.string.download_notification_title));
request.setDescription("meilishuo desc");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setVisibleInDownloadsUi(false);
// request.allowScanningByMediaScanner();
// request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
// request.setShowRunningNotification(false);
// request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
request.setMimeType("application/cn.trinea.download.file");
downloadId = downloadManager.enqueue(request);
downloadManager.enqueue是加入下載請(qǐng)求到下載管理類中,并且會(huì)返回一個(gè)下載的ID。
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
大文件只能在wifi下下載
需要注冊(cè)一個(gè)下載完成的廣播,自定義這個(gè)廣播
class CompleteReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
/**
* get the id of download which have download success, if the id is my id and it's status is successful,
* then install it
**/
long completeDownloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if (completeDownloadId == downloadId) {
initData();
updateView();
// if download successful, install apk
if (downloadManagerPro.getStatusById(downloadId) == DownloadManager.STATUS_SUCCESSFUL) {
String apkFilePath = new StringBuilder(Environment.getExternalStorageDirectory().getAbsolutePath())
.append(File.separator).append(DOWNLOAD_FOLDER_NAME).append(File.separator)
.append(DOWNLOAD_FILE_NAME).toString();
install(context, apkFilePath);
}
}
}
};
這里的intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);的DownloadManager.EXTRA_DOWNLOAD_ID是DownloadManager類里的參數(shù),使用下面方法注冊(cè)廣播
/** register download success broadcast **/ registerReceiver(completeReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
用到的IntentFilter是下載完成的Filter
然后會(huì)通知這個(gè)廣播,并且返回的intent里面包含了DownloadManager.EXTRA_DOWNLOAD_ID的參數(shù)。
關(guān)于DownloadManager的其他用法可以查看api文檔
這里再介紹下DownloadManager.Query的用法。
顯而易見Query是內(nèi)部類。有query.setFilterById和query.setFilterByStatus兩個(gè)方法,
query.setFilterById就是把downloadManager.enqueue返回的id放進(jìn)去作為查詢的條件;
可以進(jìn)行拼接查詢的條件。
Cursor cur = downloadManager.query(query);
這里用的Query查詢Downloads的數(shù)據(jù)庫(kù),但是只可以查詢本應(yīng)用下載的數(shù)據(jù)
/** * 使用DownloadManager.Query查詢Downloads的DB,但是在stackoverflow中的解釋是 * You can't access this DB from my application. For your own downloads, * use DownloadManager.Query to check on your download status. Data about downloads is not shared to other apps. */
所以要是想通過DownloadManager.Query查詢系統(tǒng)所有的下載記錄是不可行的。
但是需求有要怎么辦呢?
記得ApiDemo里有用戶聯(lián)系人使用Uri的方式查詢聯(lián)系人contacts,進(jìn)入Root Explore觀察com.android.providers.downloads包里的DB數(shù)據(jù)庫(kù)內(nèi)容時(shí),發(fā)現(xiàn)下載的記錄里有content://media/external/file/452122
這種內(nèi)容,所以可以這樣獲取到下載的文件
測(cè)試下返回的字段
/*String[] downNames = cursor.getColumnNames();
String str = "";
for(int i=0;i<downNames.length;i++){
str += downNames[i]+",";
}*/
if(cursor!=null&&cursor.moveToLast()){
// cursor.moveToPrevious()
String displayname = cursor.getString(cursor.getColumnIndex("_display_name"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String title = cursor.getString(cursor.getColumnIndex("title"));
String description = cursor.getString(cursor.getColumnIndex("description"));
String data = cursor.getString(cursor.getColumnIndex("_data"));
String bucket = cursor.getString(cursor.getColumnIndex("bucket_display_name"));
downloadTip.setText(displayname+","+name+","+title+","+description+","+data+","+bucket);
}
根據(jù)自己需求提取字段。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android基本組件用法總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android布局layout技巧總結(jié)》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- DownloadManager實(shí)現(xiàn)文件下載功能
- Android中DownloadManager實(shí)現(xiàn)文件下載實(shí)例詳解
- android中DownloadManager實(shí)現(xiàn)版本更新,監(jiān)聽下載進(jìn)度實(shí)例
- Android開發(fā)之自帶下載器DownloadManager的使用示例代碼
- 使用Android系統(tǒng)提供的DownloadManager來下載文件
- Android中使用DownloadManager類來管理數(shù)據(jù)下載的教程
- 基于DownloadManager的簡(jiǎn)單下載器編寫小結(jié)
相關(guān)文章
Android實(shí)現(xiàn)短信驗(yàn)證碼自動(dòng)攔截讀取功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)短信驗(yàn)證碼自動(dòng)攔截讀取功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
Android編程實(shí)現(xiàn)抽屜效果的方法詳解
這篇文章主要介紹了Android編程實(shí)現(xiàn)抽屜效果的方法,結(jié)合具體實(shí)例形式分析了Android實(shí)現(xiàn)抽屜效果的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-05-05
android使用SwipeRefreshLayout實(shí)現(xiàn)ListView下拉刷新上拉加載
這篇文章主要為大家詳細(xì)介紹了android使用SwipeRefreshLayout實(shí)現(xiàn)ListView下拉刷新上拉加載,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android中 TeaScreenPopupWindow多類型篩選彈框功能的實(shí)例代碼
這篇文章主要介紹了Android TeaScreenPopupWindow多類型篩選彈框功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-06-06
Android 浮動(dòng)編輯框的具體實(shí)現(xiàn)代碼
本篇文章主要介紹了Android 浮動(dòng)編輯框的具體實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10
Android觸摸事件如何實(shí)現(xiàn)筆觸畫布詳解
這篇文章主要給大家介紹了關(guān)于Android觸摸事件如何實(shí)現(xiàn)筆觸畫布的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10

