2011-07-29 3 views
0

私はプログレスバーを参照してくださいカント私はDownloadListenerを使用することが一つの問題で完全に動作している場合、ダウンロードではないにブラウザアンドロイド - DownloadListenerまたはAsyncTask

を扱うのWebViewでURLをclinkingことにより、Webページからいくつかのファイルをダウンロードしようとしています

私はそれをダウンロードするために、私は、コードでURLを配置する必要がAsyncTaskを使用する場合

私はちょうどURLをクリックしてダウンロードを開始することができます

私の質問は私が聞かせてAsyncTaskは座ってすることなく、ウェブから任意のURLをダウンロードすることができる方法であります

downloadFile.execute( "ダウンロードしたいファイルへのURL");

または私はちょうど私が私のページから

を任意のファイルをダウンロードし、私はカント時にプログレスバーを持ちたい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を開始します。

関連する問題