android - 使用下载管理器下载文件

标签 android android-download-manager

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

    }
};

请帮忙

最佳答案

终于用 2 行解决了这个问题

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

关于android - 使用下载管理器下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33020815/

相关文章:

android - kotlin 多平台 : how to reduce build time?

java - 一个物体或多个物体上的多个固定装置?

android - ActivityTestRule.getActivity 在 Before 方法中返回 null

java - 下载失败时 DownloadManager 游标属性 COLUMN_LOCAL_FILENAME 为空

android - 下载图像并将其保存在 android 中

android - 使用android下载管理器下载文件并保存在应用程序文件夹中

java - DownloadManager 完成下载后无法移动文件

android - 下载管理器不工作

java - 展开高度的高度

java - ADT Eclipse 中出现无效字符常量