Saturday, July 26, 2014

Android DownloadManager's broadcast is received two times for the same download id

                                     
Hi,

    Here I am going to explain the temporary fix for the mentioned problem with Android DownloadManager
 
My Observations:

1.  Android DownloadManager is sending broadcast more than one time for the same download id. First time if we receive STATUS_SUCCESSFUL, second time we may receive STATUS_FAILURE for the same download id. Whenever the DownloadManager sends broadcast with the uri as null we need to return from the onReceive() as shown below. This way we can solve this issue.

2. Generally DownloadManager deletes the semi-downloaded files if they were failed to download further.
    Some times it is found that DownloadManager is deleting the successfully downloaded files as well. So
    to come out of the problem copy the downloaded file to another location as shown below.

Temporay solutions:
1. In your application BroadcastReceiver's class onReceive method, do the following

      public class DownloadReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action != null) {
                if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
                    long downloadId = intent.getLongExtra(
                            DownloadManager.EXTRA_DOWNLOAD_ID, 0);
                    DownloadManager downloadMgr = (DownloadManager) context
                            .getSystemService(context.DOWNLOAD_SERVICE);
                    Uri uri = downloadMgr.getUriForDownloadedFile(downloadId);
                    if(uri != null) {
                              //Do your stuff here
                              Query query = new Query();
                              query.setFilterById(downloadId);
                              Cursor cursor = downloadMgr.query(query);
                              if(cursor != null && cursor.moveToFirst()) {
                                       //Get the status of the download
                                      int status = cursor
                                    .getInt(cursor
                                            .getColumnIndex(DownloadManager.COLUMN_STATUS));
                                      //To Check how many bytes download
                                      int bytes = cursor
                                    .getInt(cursor
                                            .getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
                                      //Gives the downloaded file location.
                                      String downloadedLocation = cursor
                                    .getString(cursor
                                            .getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
                                      //Copy the downloaded file to  another location
                                      FileUtil.copy(downloadedLocation, <target_location_path);
                              }
                    }
                 }
             }
           }
      }
 



No comments:

Post a Comment