Use of Android DownloadManager

Reprinted from the
link : https://www.jianshu.com/p/5b1ad56b9cef

Android itself uses Volley to handle lightweight network requests, but it is not recommended for downloading large files. When looking for how to download large data, Android officials gave a suggestion to use DownloadManager to handle it.

Volley is not suitable for large download or streaming operations, since Volley holds all responses in memory during parsing. For large download operations, consider using an alternative like DownloadManager.

Use DownloadManager to download

I don't want to detail what methods it has and what parameters are available to developers, because I want you to focus on how the DownloadManager starts the download. First assume that we need to download an apk to update the version of the application, then let's see how to use DownloadManager to deal with it:

// uri 是你的下载地址,可以使用Uri.parse("http://")包装成Uri对象
DownloadManager.Request req = new DownloadManager.Request(uri);

// 通过setAllowedNetworkTypes方法可以设置允许在何种网络下下载,
// 也可以使用setAllowedOverRoaming方法,它更加灵活
req.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);

// 此方法表示在下载过程中通知栏会一直显示该下载,在下载完成后仍然会显示,
// 直到用户点击该通知或者消除该通知。还有其他参数可供选择
req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

// 设置下载文件存放的路径,同样你可以选择以下方法存放在你想要的位置。
// setDestinationUri
// setDestinationInExternalPublicDir
req.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS, title);

// 设置一些基本显示信息
req.setTitle("Android.apk");
req.setDescription("下载完后请点击打开");
req.setMimeType("application/vnd.android.package-archive");

// Ok go!
DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
long downloadId = dm.enqueue(req);

So we start the download. As for resuming the upload from a breakpoint and under what network environment to download, the system is already running the way we want, so we don't need to worry about these.

Get download file

See the parameter downloadId returned in the last line of the code above? Yes, we need to use this to find out the file we just downloaded:

DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Query query = new DownloadManager.Query().setFilterById(downloadId);
Cursor c = dm.query(query);
if (c != null) {
    if (c.moveToFirst()) {
        String fileUri = c.getString(c.getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_URI));
        // TODO 你可以在这里处理你的文件
    }
    c.close();
}

Get the status of the download

Sometimes we don't want to download the same file repeatedly, or for some reason, we need to query the status of the download in order to update the interface and other operations, then let's see how to get the status of the download:

DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Query query = new DownloadManager.Query().setFilterById(downloadId);
Cursor c = dm.query(query);
if (c != null && c.moveToFirst()) {
    int status = c.getInt(c.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS));
    switch (status) {
        case DownloadManager.STATUS_PENDING:
            break;
        case DownloadManager.STATUS_PAUSED:
            break;
        case DownloadManager.STATUS_RUNNING:
            break;
        case DownloadManager.STATUS_SUCCESSFUL:
            break;
        case DownloadManager.STATUS_FAILED:
            break;
}
if (c != null) {
    c.close();
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325713515&siteId=291194637