2011-07-26 53 views
1

我正在编写一个应用程序,其中我想检测下载是否已启动并检索正在下载的文件的URI,然后取消下载下载管理器。我正在这样做,以便我可以将此URI发送到其他地方。如何从Android下载管理器下载文件的网络uri

麻烦的是,当下载通过查询下载管理器开始我可以检测,但有下载

+1

我当然希望你所要做的是不可能的,因为这将是一个严重的安全漏洞。 – CommonsWare

回答

3

的方法或在下载管理器中的常量变量从中我也能获得文件的URL确定它的怪异回答你自己的问题,但我终于想出了如何做到这一点。 android.app中有一个DownloadManager类,它存储所有启动的http下载列表及其状态。这些可以根据下载是“正在运行”,“正在等待”,“已暂停”等过滤掉。

该列表可以读入游标,结果的其中一列是'COLUMN_URI',它是正在下载文件的URL。在那里我已经用它的样本代码,如下所示:

public void readDownloadManager() { 
       DownloadManager.Query query = null; 
       DownloadManager downloadManager = null; 
       Cursor c = null; 
       try { 
        query = new DownloadManager.Query(); 
        downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE); 

        //Just for testing I initiated my own download from this url. When an http 
        // reuest for this url is made, since download is taking place, it gets saved in 
        // the download manager. 
        Request request = new Request(Uri.parse("http://ocw.mit.edu/courses" + 
          "/aeronautics-and-astronautics/16-100-aerodynamics-fall-2005" + 
          "/lecture-notes/16100lectre1_kvm.pdf")); 
        downloadManager.enqueue(request); 
        query.setFilterByStatus(DownloadManager.STATUS_PENDING); 
        c = downloadManager.query(query); 

        if(true){ 
         int statusColumnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS); 
         int urlColumnIndex = c.getColumnIndex(DownloadManager.COLUMN_URI); 
         long downloadProcessIdColumnNo = c.getColumnIndex(DownloadManager.COLUMN_ID); 
         Log.d("Column Count", ((Integer)c.getCount()).toString()); 
         if(c.getCount() > 0){ 
          String url=""; 
          c.moveToLast(); 
          if(c.isLast()){ 
           url = c.getString(urlColumnIndex); 
           downloadManager.remove(downloadProcessIdColumnNo); 
           Log.d("Count after remove", ((Integer)c.getCount()).toString()); 
          } 
          Log.d("After", "Stopped Working"); 

          //Here I am sending the url to another activity, where I can work with it. 
          Intent intent = new Intent(EasyUploadMainMenu.this, EasyUploadActivity.class); 
          Bundle b = new Bundle(); 
          b.putString("url", url); 
          intent.putExtras(b); 
          startActivity(intent); 
          Log.d("url:", url); 
         } 
        } 

       } catch (NullPointerException ex) { 
        ex.printStackTrace(); 
       } 
      } 
+0

幸运的是,这项技术似乎只适用于您自己的流程下载。 – CommonsWare

相关问题