使用下载管理器下载文件

8
我将使用DownloadManager类从服务器下载文件。
以下是我的操作步骤:
public void downloadPdf(String url, String sem, String title, String branch) {
    Uri Download_Uri = Uri.parse(url);
    DownloadManager.Request request = new DownloadManager.Request(Download_Uri);

    //Restrict the types of networks over which this download may proceed.
    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
    //Set whether this download may proceed over a roaming connection.
    request.setAllowedOverRoaming(false);
    //Set the title of this download, to be displayed in notifications (if enabled).
    request.setTitle("Downloading");
    //Set a description of this download, to be displayed in notifications (if enabled)
    request.setDescription("Downloading File");
    //Set the local destination for the downloaded file to a path within the application's external files directory
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, title + "_" + branch + "_" + sem + "Year" + System.currentTimeMillis() + ".pdf");

    //Enqueue a new download and same the referenceId
    downloadReference = downloadManager.enqueue(request);
}

当下载完成后,如果用户点击通知,则应打开该文件,我在这段代码中该怎么做?

 BroadcastReceiver onNotificationClick = new BroadcastReceiver() {
    public void onReceive(Context ctxt, Intent intent) {

    }
};

请帮忙

1个回答

5
最终通过只有两行代码的方式解决了这个问题。
 request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

不要使用 VISIBILITY_VISIBLE_NOTIFY_COMPLETED,而应该使用这个标志 VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION - pratham kesarkar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接