在我的应用中,我使用
下载了一些文件 DownloadManager downloadManagerStore = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri downloadUri = Uri.parse(queryURL);
DownloadManager.Request request = new DownloadManager.Request(downloadUri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false) .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)
.setDestinationInExternalPublicDir(dir);
downloadManagerStore.enqueue(request);
我使用BroadcastReceiver
管理文件,这是代码:
@Override
public void onReceive(Context context, Intent intent) {
this.context = context;
//do something
}
但我做了两次不同的下载,当他们完成BroadcastReceiver
运行时,但我怎么知道哪个下载完成了呢?
答案 0 :(得分:1)
将您的下载ID保存在偏好设置中(最优选地)或enqueue()
下载请求时的任意位置
long downloadId = downloadManagerStore.enqueue(request);
然后在您的接收器中,当下载完成后,您可以将保存的downloadId
与DownloadManager.EXTRA_DOWNLOAD_ID
匹配,如下所示 -
@Override
public void onReceive(Context context, Intent intent) {
this.context = context;
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
//Check your saved "downloadId" with "id" and perform your task depending on that
}
}