检查下载活跃 [英] Check if downloads are active
问题描述
所以我使用下载管理器下载我的应用程序的多个文件。我需要这些文件在启动某项活动之前完成下载。如何检查是否有活动的下载,这样我就可以告诉用户等待下载完成。然后,他们完成的时候,我需要一个按钮可见。我GOOGLE了这一点,甚至尝试了一些code自己(盲目),并没有什么作品。如果有人能轻推我的方向是正确的,我会感激不尽。
So I'm using Download Manager to download multiple files in my app. I need these files to finish downloading before starting a certain activity. How can I check if there are active downloads, so I can tell the user to wait until the downloads are finished. And then, when they are finished, I need to make a button visible. I've googled this, even tried some code myself(blindly) and nothing works. If somebody can nudge me in the right direction I'd be grateful.
推荐答案
使用<一个href="http://developer.android.com/reference/android/app/DownloadManager.html#query%28android.app.DownloadManager.Query%29"><$c$c>query()$c$c>打听下载。当你调用<一个href="http://developer.android.com/reference/android/app/DownloadManager.html#enqueue%28android.app.DownloadManager.Request%29"><$c$c>enqueue()$c$c>,返回值是用于下载的ID。您可以通过状态的查询:
Use query()
to inquire about downloads. When you call enqueue()
, the return value is an ID for the download. You can query by status as well:
Cursor c = downloadManager.query(new DownloadManager.Query()
.setFilterByStatus(DownloadManager.STATUS_PAUSED
| DownloadManager.STATUS_PENDING
| DownloadManager.STATUS_RUNNING));
要当下载完成后得到通知,注册的BroadcastReceiver
为 ACTION_DOWNLOAD_COMPLETE
:
To be notified when a download is finished, register a BroadcastReceiver
for ACTION_DOWNLOAD_COMPLETE
:
BroadcastReceiver onComplete = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// do something
}
};
registerReceiver(onComplete, new IntentFilter(
DownloadManager.ACTION_DOWNLOAD_COMPLETE));
请注意,您还应该监听 ACTION_NOTIFICATION_CLICKED
广播知道,当用户点击该通知正在运行的下载。
Note that you should also listen for the ACTION_NOTIFICATION_CLICKED
broadcast to know when a user has clicked the notification for a running download.
这篇关于检查下载活跃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!