Android下载监听器

时间:2012-11-15 17:55:34

标签: java android

有没有办法找出下载是否成功。 我正在使用以下代码从其他手机p2p下载文件,但有些时候其他用户超出了wifi范围。我想知道源是超出范围所以那时下载是不可能的。

是否有任何可以帮助我的听众类型的东西?

Uri uri=Uri.parse(downLoadLink);

Environment
  .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
  .mkdirs();

lastDownload = mgr.enqueue(new DownloadManager.Request(uri)
  .setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
  .setAllowedOverRoaming(false)
  .setTitle(aryToReturn.get(position).name)
  .setDescription("File Downloding...")
  .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,aryToReturn.get(position).name));

1 个答案:

答案 0 :(得分:0)

我使用以下代码完成了此操作

  private class downloadTask extends AsyncTask<String, Void, String> {
  ProgressDialog dialog = new ProgressDialog(webServices.this);
  @Override
  protected String doInBackground(String... params) {

      //Uri uri=Uri.parse("http://commonsware.com/misc/test.mp4"); 
      Uri uri=Uri.parse(downLoadLink);

        Environment
          .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
          .mkdirs();

        lastDownload=
          mgr.enqueue(new DownloadManager.Request(uri)
                      .setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |
                                              DownloadManager.Request.NETWORK_MOBILE)
                      .setAllowedOverRoaming(false)
                      .setTitle(downloadFileName)
                      .setDescription("File Downloding...")
                      .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
                              downloadFileName));
                   // Log.d("-->", String.valueOf(lastDownload));
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "Executed";
  }      

  @Override
  protected void onPostExecute(String result) {



     dialog.dismiss();
    Cursor c=mgr.query(new DownloadManager.Query().setFilterById(lastDownload));

    if (c==null) {
      //Toast.makeText(this, "Download not found!", Toast.LENGTH_LONG).show();
    }
    else {
      c.moveToFirst();
      Log.e("yes", statusMessage(c));
     // Toast.makeText(this, statusMessage(c), Toast.LENGTH_LONG).show();
    }

  }

  @Override
  protected void onPreExecute() {
     dialog.setMessage("Please wait...");
        dialog.setCancelable(false);
        dialog.show();
     }

  @Override
  protected void onProgressUpdate(Void... values) {
  }


  }


private String statusMessage(Cursor c) {
String msg="???";

switch(c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS))) {
  case DownloadManager.STATUS_FAILED:
    msg="Download failed!";
    break;

  case DownloadManager.STATUS_PAUSED:
    msg="Download paused!";
    break;

  case DownloadManager.STATUS_PENDING:
    msg="Download pending!";
    break;

  case DownloadManager.STATUS_RUNNING:
    msg="Download in progress!";
    break;

  case DownloadManager.STATUS_SUCCESSFUL:
    msg="Download complete!";
    break;

  default:
    msg="Download is nowhere in sight";
    break;
}

return(msg);

}