2016-04-20 46 views
0

在我的應用程序,我下載與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運行,但我怎麼知道哪個下載完成?

+0

http://developer.android.com/reference/android/app/DownloadManager.html#EXTRA_DOWNLOAD_ID – CommonsWare

回答

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 
      }  
} 
+0

謝謝!你幫我。 –