简体   繁体   English

Android下载列表器

[英]Android Download Listner

Is there any way to find out that the download is successfully or not. 有什么方法可以确定下载成功与否。 i am using following code to download files from other mobiles phones p2p, but some time other user is out of wifi range. 我正在使用以下代码从其他手机p2p下载文件,但有时其他用户不在wifi范围内。 the i wanted to know that the source is out of range so download is not possible at that time. 我想知道来源超出范围,因此当时无法下载。

is there any listener type thing that can help me ? 有没有可以帮助我的听众类型的东西?

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

I have done this using following code 我使用以下代码完成了此操作

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 android listner中的Java层次结构 - Java hierarchy inside an android listner android后台服务中的位置列表器 - Location listner in android background service 动作列表器不适用于带有DragLayout的android中的图像 - Action listner not working on image in android with draglayout 在构造函数中实现了监听器的 (java/android) class 是否应该有 static 方法,这些方法与监听器操作有某种关联? - Should a (java/android) class that has a listner implemented in the constructor have static methods which are somehow related to the listner actions? Android:具有基本适配器的自定义列表视图:如何在点击列表器中设置行项目? - Android: custom List view with base adapter: How to set on click listner for row items? 在Android中下载 - Download With In Android 使用下载管理器 Android 下载失败 9 - Unsuccessful download with Download Manager Android 9 Onclick listner to expandable listview - Onclick listner to expandable listview 我想制作一个点击监听器,当我从下拉菜单中选择一个项目时,它会转到 android studio 中的所需页面 - I want to make an on click listner in such a way that when i select an item from drop down menu it goes to the desired page in android studio 编辑器动作列表器上的Java - java on editor action listner
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM