1

有什么办法可以知道下载成功与否。我正在使用以下代码从其他手机 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));
4

1 回答 1

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

}

于 2012-11-15T20:20:48.753 回答