java - Android DownloadManager 获取文件名

标签 java android broadcastreceiver android-download-manager receiver

在我的应用程序中,您可以下载一些文件。我使用 Android DownloadManager 类进行下载。下载完成后,它应该向我显示文件已下载的消息。问题是,可能同时有 2,3 或 4 个下载。我的 BroadcastReceiver 代码如下所示:

receiver_complete = new BroadcastReceiver(){
         @Override
          public void onReceive(Context context, Intent intent) {
             String action = intent.getAction();
                if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE) ){
                    Toast.makeText(MainActivity.this, MainActivity.this.getString(R.string.download_finished, "Here should be the name", Toast.LENGTH_SHORT).show();
                }
         }
     };

如何获取已完成下载的当前文件名?

非常感谢。

最佳答案

我认为您想在 if block 中放入类似的内容。将 YOUR_DM 替换为您的 DownloadManager 实例。

Bundle extras = intent.getExtras();
DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(extras.getLong(DownloadManager.EXTRA_DOWNLOAD_ID));
Cursor c = YOUR_DM.query(q);

if (c.moveToFirst()) {
    int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
    if (status == DownloadManager.STATUS_SUCCESSFUL) {
        // process download
        title = c.getString(c.getColumnIndex(DownloadManager.COLUMN_TITLE));
        // get other required data by changing the constant passed to getColumnIndex
    }
}

关于java - Android DownloadManager 获取文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13321984/

相关文章:

java - 如何在 Java Web 应用程序中同时使用 http 服务器和应用程序服务器

android - 编辑文本还是 TextView ?对于 “copy” 和 “share” 选项

android - 如何更改 ViewModel 中 xml 对象的可见性?

android - 如何构建一个提醒用户的 Android 应用程序,即使它被杀死了?

java - 如何将日期变量转换为java.sql.date

java - 如何使用 CAPTURE 绑定(bind)创建 AST?

java - 将类名添加到uibinder xml文件中的元素

android - context.openFileInput() 尝试访问存储的文件时返回 null

java - BroadcastReceiver 被系统杀死或取消注册

安卓 map 框 : How to unregister IntentReceiver?