2011-07-29 59 views
0

正在試圖通過無比的URL與web視圖處理下載而不是瀏覽器的Android - DownloadListener或的AsyncTask

如果我使用DownloadListener它有一個問題,完美的作品,我不能看到進度條從網頁上下載一些文件

如果我使用的AsyncTask我已經把該URL代碼下載它,我可以只需點擊鏈接並開始下載

我的問題是我怎樣才能讓的AsyncTask從網絡上下載的任何URL,而不坐

downloadFile.execute(「您要下載的文件的網址」);

或如何我可以創建DownloadListener

@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    webview = (WebView) findViewById(R.id.webview); 
    myProgressBar = (ProgressBar) findViewById(R.id.progressbar_Horizontal); 
    new Thread(myThread).start(); 
    webview.setWebViewClient(new HelloWebViewClient()); 
    webview.getSettings().setJavaScriptEnabled(true); 
    webview.setInitialScale(50); 
    webview.getSettings().setUseWideViewPort(true); 
    webview.setVerticalScrollBarEnabled(false); 
    webview.setHorizontalScrollBarEnabled(false); 
    webview.loadUrl("http://localhost/index.php"); 
    webview.setWebViewClient(new DownloadWebViewClient()); 
    webview.setDownloadListener(new DownloadListener() { 

     @Override 
     public void onDownloadStart(String url, String userAgent, 
      String contentDisposition, String mimetype, 
      long contentLength) { 
      InputStream is; 
      try { 
       URL u = new URL(url); 
       HttpURLConnection con = (HttpURLConnection) u.openConnection(); 
       con.setRequestMethod("GET"); 
       con.setDoOutput(true); 
       con.connect(); 
       is = con.getInputStream(); 


      // Path and File where to download the APK 
      String path = Environment.getExternalStorageDirectory() + "/apdroid/"; 
      String fileName = url.substring(url.lastIndexOf('/') + 1); 
      File dir = new File(path); 
      dir.mkdirs(); // creates the download directory if not exist 
      File outputFile = new File(dir, fileName); 
      FileOutputStream fos = new FileOutputStream(outputFile); 

      // Save file from URL to download directory on external storage 
      byte[] buffer = new byte[1024]; 
      int len = 0; 
      while ((len = is.read(buffer)) != -1) { 
       fos.write(buffer, 0, len); 
      } 
      fos.close(); 
      is.close(); 


      Intent intent = new Intent(Intent.ACTION_VIEW); 
      String name = Environment.getExternalStorageDirectory() + "/apdroid/" + url.substring(url.lastIndexOf('/') + 1); 
      intent.setDataAndType(Uri.fromFile(new File(name)), "application/vnd.android.package-archive"); 
      startActivity(intent); 


     }catch (IOException e) { 
      e.printStackTrace(); 
     } 
     } 
    }); 


} 



protected void install(String fileName) { 
    // TODO Auto-generated method stub 

} 



private Runnable myThread = new Runnable() { 

    @Override 
    public void run() { 
     while (myProgress < 100) { 
      try { 
      myHandle.sendMessage(myHandle.obtainMessage()); 
      Thread.sleep(1000); 
      } catch (Throwable t) { 
      } 
     } 
    } 

    Handler myHandle = new Handler() { 

     @Override 
     public void handleMessage(Message msg) { 
      myProgress++; 
      myProgressBar.setProgress(myProgress); 
     } 

    }; 

}; 









private class HelloWebViewClient extends WebViewClient { 










@Override 
public void onReceivedError(WebView view,int errorCode,String description,String failingUrl) { 
    try {view.stopLoading();} catch(Exception e){} 
    try {view.clearView();} catch(Exception e){} 
    view.loadUrl("file:///android_asset/wifi.html"); 
} 
    } 

我只是想有進度條,當我從我的網頁下載任何文件

,我不能進度使用asyncTask,因爲我不得不通過點擊文件在代碼中的文件

+2

難以從問題中提取您的要求...? – ngesh

+0

抱歉,如果我不夠清楚 –

回答

0

您應該可能是overriding the URL loading進程,並通過某種方式識別是否正在加載其您要下載其資源的URL。
只要您檢測到這一點,就停止加載頁面並使用此URL啓動AsyncTask。