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;
    }

你可能感兴趣的:(DownloadManagerAPI和封装)