如何在下载接收器中访问本地文件名? [英] How to get access to local file name in download receiver?
本文介绍了如何在下载接收器中访问本地文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试获取下载文件的文件路径,我提供了可以正常工作的接收器,但是我如何获取文件名/路径?
I'm trying to get file path of downloaded file, i provided receiver which works but how can i get file name/path?
ononReceive里面
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
DownloadManager.Query q = new DownloadManager.Query();
Cursor c = this.query(q); // how to get access to this since there is no instance of DownloadManager
try {
String filePath = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
Log.i("DOWNLOAD LISTENER", filePath);
} catch(Exception e) {
} finally {
c.close();
}
}
无法解析方法查询(...)
Cannot resolve method query(...)
推荐答案
您可以持有<$通过 Context
的 getSystemService()
方法访问c $ c> DownloadManager 实例。
You could get ahold of a DownloadManager
instance via the getSystemService()
method of Context
.
类似这样的方法应该起作用:
Something like this should work:
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
// get the DownloadManager instance
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Query q = new DownloadManager.Query();
Cursor c = manager.query(q);
if(c.moveToFirst()) {
do {
String name = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
Log.i("DOWNLOAD LISTENER", "file name: " + name);
} while (c.moveToNext());
} else {
Log.i("DOWNLOAD LISTENER", "empty cursor :(");
}
c.close();
}
}
这篇关于如何在下载接收器中访问本地文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文