コード例 #1
0
  /**
   * 下载一个任务
   *
   * @param url
   * @param listener
   */
  public void addTask(String url, DownloadListener listener) {
    if (StringUtils.isEmpty(url)) {
      Logger.d("download url null");
      return;
    }

    DownloadInfo downloadInfo = new DownloadInfo();
    downloadInfo.setUrl(url);
    if (!hasTask(url)) {
      downloadInfo.setTargetFolder(mTargetFolder);
      try {
        mDbHelper.save(downloadInfo);
      } catch (DbException e) {
        Logger.e(e);
      }

      if (mDownloadingTasks.size() < MAX_DOWNLOAD_COUNT) {
        downloadInfo.setState(DownloadInfo.DOWNLOADING);
        mDownloadingTasks.add(downloadInfo);
        DownloadHttpTask task =
            new DownloadHttpTask(downloadInfo, mDownloadUIHandler, mDbHelper, this);
        mDownloadingTaskMap.put(url, task);
        task.start();
      } else { // 加入等待队列
        downloadInfo.setState(DownloadInfo.WAIT);
        boolean b = mWaitTasks.offer(downloadInfo);
        if (b) {
          addTaskListener(url, listener);
        }
      }
      mAllTasks.add(downloadInfo);
    } else {
      Logger.d("任务已存在");
    }
  }
コード例 #2
0
 private void executeNextTask() {
   if (mDownloadingTasks.size() < MAX_DOWNLOAD_COUNT) {
     if (mWaitTasks.size() > 0) {
       DownloadInfo downloadInfo = mWaitTasks.poll();
       if (downloadInfo != null) {
         String url = downloadInfo.getUrl();
         downloadInfo.setState(DownloadInfo.DOWNLOADING);
         DownloadHttpTask task =
             new DownloadHttpTask(downloadInfo, mDownloadUIHandler, mDbHelper, this);
         mDownloadingTaskMap.put(url, task);
         task.start();
       }
     }
   } else {
     Logger.d("已达到最大下载数量:" + mDownloadingTasks.size());
   }
 }