DownloadManagerAPI和封裝

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.Query

  • setFilterByStatus(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;
    }
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 從初中接觸英語到現在,聽到的最多的英語學習方法就大聲朗讀。當時風靡全國的瘋狂英語創始人李陽就是心目中的偶像,那時的...
    下班后泡英文閱讀 428評論 0 0
  • 還有一天就要開學了,相信很多家長都為孩子作業擔心吧,我也不例外。 前天我提醒孩子還有2天就開學了,孩子說知道了。繼...
    空心菜_9e82閱讀 588評論 0 2
  • 中秋節期間,我利用時間和我的助理在市場上進行了實地調研,目前保險越來越普及,而我們普通人士對保險又是怎么理解或還有...
    金虞閱讀 518評論 2 4
  • 只為自己沒有做過的事情后悔,不為自己做過的事情后悔
    姜雷_24b1閱讀 192評論 0 0