How to notify after all download completed from Android download manager

坚强是说给别人听的谎言 提交于 2020-01-04 07:46:08

问题


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();
}}

回答1:


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...
    }



回答2:


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);


来源:https://stackoverflow.com/questions/43514566/how-to-notify-after-all-download-completed-from-android-download-manager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!