Android |发送"额外"到BroadcastReceiver

时间:2016-04-20 21:47:47

标签: android

在我的应用中,我使用

下载了一些文件
        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运行时,但我怎么知道哪个下载完成了呢?

1 个答案:

答案 0 :(得分:1)

将您的下载ID保存在偏好设置中(最优选地)或enqueue()下载请求时的任意位置

long downloadId = downloadManagerStore.enqueue(request);

然后在您的接收器中,当下载完成后,您可以将保存的downloadIdDownloadManager.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
          }    
}