Android DownloadManager获取文件名

在我的应用中,您可以下载一些文件。我使用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块中放入类似的内容。替换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

}

}

以上是 Android DownloadManager获取文件名 的全部内容, 来源链接: utcz.com/qa/420260.html

回到顶部