【问题标题】:Download Files Using download manager使用下载管理器下载文件
【发布时间】:2016-01-06 08:56:04
【问题描述】:

我正在使用 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) {

    }
};

请帮忙

【问题讨论】:

    标签: android android-download-manager


    【解决方案1】:

    最后只用了两行就解决了这个问题

     request.allowScanningByMediaScanner();
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    

    【讨论】:

    • 不要使用VISIBILITY_VISIBLE_NOTIFY_COMPLETED,而是使用这个标志VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION
    猜你喜欢
    • 2018-06-10
    • 2013-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多