在我的应用程序中,您可以下载一些文件。我使用 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/