這篇文章將為大家詳細講解有關Android如何基于自帶的DownloadManager實現下載功能,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
具體如下:
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是加入下載請求到下載管理類中,并且會返回一個下載的ID。
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
大文件只能在wifi下下載
需要注冊一個下載完成的廣播,自定義這個廣播
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類里的參數,使用下面方法注冊廣播
/** register download success broadcast **/ registerReceiver(completeReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
用到的IntentFilter是下載完成的Filter
然后會通知這個廣播,并且返回的intent里面包含了DownloadManager.EXTRA_DOWNLOAD_ID的參數。
關于DownloadManager的其他用法可以查看api文檔
這里再介紹下DownloadManager.Query的用法。
顯而易見Query是內部類。有query.setFilterById和query.setFilterByStatus兩個方法,
query.setFilterById就是把downloadManager.enqueue返回的id放進去作為查詢的條件;
復制代碼 代碼如下:
query.setFilterByStatus(DownloadManager.STATUS_FAILED|DownloadManager.STATUS_PENDING|DownloadManager.STATUS_RUNNING|DownloadManager.STATUS_SUCCESSFUL);
可以進行拼接查詢的條件。
Cursor cur = downloadManager.query(query);
這里用的Query查詢Downloads的數據庫,但是只可以查詢本應用下載的數據
/** * 使用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查詢系統所有的下載記錄是不可行的。
但是需求有要怎么辦呢?
記得ApiDemo里有用戶聯系人使用Uri的方式查詢聯系人contacts,進入Root Explore觀察com.android.providers.downloads包里的DB數據庫內容時,發現下載的記錄里有content://media/external/file/452122
這種內容,所以可以這樣獲取到下載的文件
復制代碼 代碼如下:
Cursor cursor = getContentResolver().query(Uri.parse("content://media/external/file"), null, null, null, null);
測試下返回的字段
/*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);
}根據自己需求提取字段。
關于“Android如何基于自帶的DownloadManager實現下載功能”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。