venura
venura

Reputation: 125

How to notify after all download completed from Android download manager

I'm using Android download manager class. I need to write "Download Complete" after all downloads are completed. I have tried something and it's working. But in here it's writing after file by file completed. I need to write only once (After all are completed). I also tried without a cursor. But I failed to achieve.

public class DownloadReceiver extends BroadcastReceiver {


Context context;
@Override
public void onReceive(Context context, Intent intent) {
    long receivedID = intent.getLongExtra(
             DownloadManager.EXTRA_DOWNLOAD_ID, -1L);
             DownloadManager mgr = (DownloadManager)
            context.getSystemService(Context.DOWNLOAD_SERVICE);

    DownloadManager.Query query = new DownloadManager.Query();
    query.setFilterById(receivedID);
    Cursor cur = mgr.query(query);
    int index = cur.getColumnIndex(DownloadManager.COLUMN_STATUS);
    if(cur.) {
        String  filePath = cur.getString(cur.getColumnIndex(DownloadManager.COLUMN_TITLE));
        if(cur.getInt(index) == DownloadManager.STATUS_SUCCESSFUL){
            DatabaseHandler dh = new DatabaseHandler(context);
            dh.addDownloadComplete(new DownloadComplete("Download Complete"));
        }
    }
    cur.close();
}}

Upvotes: 2

Views: 3858

Answers (2)

Merhawi Fissehaye
Merhawi Fissehaye

Reputation: 2807

Here is what I think would work. Define a function as follows:

public boolean areAllDownloadsComplete(int[] downloadIds) {

    DownloadManager mgr = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);

    DownloadManager.Query query = new DownloadManager.Query();

    for(int i = 0; i < downloadIds.length; i++) {

        query.setFilterById(downloadIds[i]);

        Cursor cur = mgr.query(query);

        if(cur.moveToNext()) {

            if(cur.getInt(cur.getColumnIndex(DownloadManager.COLUMN_STATUS)) != DownloadManager.STATUS_SUCCESSFUL) {

                return false;
            }
        }
    }

    return true;
}

call this function inside the broadcast (at every download complete notification) as follows:

if(areAllDownloadsComplete(THE_DOWNLOAD_IDS)) {
    DatabaseHandler dh = new DatabaseHandler(context);
    dh.addDownloadComplete(new DownloadComplete("Download Complete"));
}

You can pass the download ids to your broadcast receiver intent as

Bundle bundle = new Bundle();
bundle.putIntArray("download_ids", THE_DOWNLOAD_IDS);
intent.putExtra(bundle);

Upvotes: 0

Vijay Pal Vishwakarma
Vijay Pal Vishwakarma

Reputation: 731

Add a check before notifying all downloads are completed. like

query.setFilterByStatus(DownloadManager.STATUS_PAUSED |
      DownloadManager.STATUS_PENDING |
      DownloadManager.STATUS_RUNNING);
    Cursor cursor = downloadManager.query(query);
    if (cursor != null && cursor.getCount() > 0) {
      return;
    }else {
      query.setFilterById(receivedID);
      //proceed your...
    }

Upvotes: 1

Related Questions