在作美圖欣賞Android應用的時候,其中有涉及到Android應用下載的功能,這個應用自己其實也比較簡單,就是經過WebView控制調用相應的WEB頁面進行展現。剛開始覺得和普通的文件下載實現,只須要一個連接,而後點擊就能夠實現下載了,但是放到手機上試的時候,點擊下載連接一點反應都沒有,在普通頁面裏面點擊是好的,且點擊其它的普通連接是能夠正常工做的。原來是由於WebView默認沒有開啓文件下載的功能,若是要實現文件下載的功能,須要設置WebView的DownloadListener,經過實現本身的DownloadListener來實現文件的下載。具體操做以下:javascript
一、設置WebView的DownloadListener:html
webView.setDownloadListener(new MyWebViewDownLoadListener());java
二、實現MyWebViewDownLoadListener這個類,具體能夠以下這樣: android
- private class MyWebViewDownLoadListener implements DownloadListener {
-
- @Override
- public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype,
- long contentLength) {
- Uri uri = Uri.parse(url);
- Intent intent = new Intent(Intent.ACTION_VIEW, uri);
- startActivity(intent);
- }
-
- }
這只是調用系統中已經內置的瀏覽器進行下載,尚未WebView自己進行的文件下載,不過,這也基本上知足咱們的應用場景了。web
我在項目中的運用
項目要求這樣:
1,須要使用WebView加載一個網頁;
2,網頁中有文件下載的連接,點擊後須要下載文件到SDcard;
3,而後自動打開文件;
下面是具體解決辦法
第一步,對WebView進行一系列設置。 瀏覽器
- WebView webview=(WebView)layout.findViewById(R.id.webview);
- webview.getSettings().setJavaScriptEnabled(true);
- webview.setWebChromeClient(new MyWebChromeClient());
- webview.requestFocus();
-
- webview.loadUrl(jcrs_sub.get(position).addr);
-
- webview.setWebViewClient(new MyWebViewClient());
- webview.setDownloadListener(new MyWebViewDownLoadListener());
-
-
- public class MyWebViewClient extends WebViewClient {
-
-
- public boolean shouldOverviewUrlLoading(WebView view, String url) {
- L.i("shouldOverviewUrlLoading");
- view.loadUrl(url);
- return true;
- }
-
- public void onPageStarted(WebView view, String url, Bitmap favicon) {
- L.i("onPageStarted");
- showProgress();
- }
-
- public void onPageFinished(WebView view, String url) {
- L.i("onPageFinished");
- closeProgress();
- }
-
- public void onReceivedError(WebView view, int errorCode,
- String description, String failingUrl) {
- L.i("onReceivedError");
- closeProgress();
- }
- }
-
-
-
- public boolean onKeyDown(int keyCode, KeyEvent event) {
-
-
-
-
- return false;
- }
第二步,起線程開始下載文件。 緩存
-
- private class MyWebViewDownLoadListener implements DownloadListener {
-
- @Override
- public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype,
- long contentLength) {
- if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
- Toast t=Toast.makeText(mContext, "須要SD卡。", Toast.LENGTH_LONG);
- t.setGravity(Gravity.CENTER, 0, 0);
- t.show();
- return;
- }
- DownloaderTask task=new DownloaderTask();
- task.execute(url);
- }
-
- }
-
- private class DownloaderTask extends AsyncTask<String, Void, String> {
-
- public DownloaderTask() {
- }
-
- @Override
- protected String doInBackground(String... params) {
-
- String url=params[0];
-
- String fileName=url.substring(url.lastIndexOf("/")+1);
- fileName=URLDecoder.decode(fileName);
- Log.i("tag", "fileName="+fileName);
-
- File directory=Environment.getExternalStorageDirectory();
- File file=new File(directory,fileName);
- if(file.exists()){
- Log.i("tag", "The file has already exists.");
- return fileName;
- }
- try {
- HttpClient client = new DefaultHttpClient();
-
- HttpGet get = new HttpGet(url);
- HttpResponse response = client.execute(get);
- if(HttpStatus.SC_OK==response.getStatusLine().getStatusCode()){
- HttpEntity entity = response.getEntity();
- InputStream input = entity.getContent();
-
- writeToSDCard(fileName,input);
-
- input.close();
-
- return fileName;
- }else{
- return null;
- }
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
-
- @Override
- protected void onCancelled() {
-
- super.onCancelled();
- }
-
- @Override
- protected void onPostExecute(String result) {
-
- super.onPostExecute(result);
- closeProgressDialog();
- if(result==null){
- Toast t=Toast.makeText(mContext, "鏈接錯誤!請稍後再試!", Toast.LENGTH_LONG);
- t.setGravity(Gravity.CENTER, 0, 0);
- t.show();
- return;
- }
-
- Toast t=Toast.makeText(mContext, "已保存到SD卡。", Toast.LENGTH_LONG);
- t.setGravity(Gravity.CENTER, 0, 0);
- t.show();
- File directory=Environment.getExternalStorageDirectory();
- File file=new File(directory,result);
- Log.i("tag", "Path="+file.getAbsolutePath());
-
- Intent intent = getFileIntent(file);
-
- startActivity(intent);
-
- }
-
- @Override
- protected void onPreExecute() {
-
- super.onPreExecute();
- showProgressDialog();
- }
-
- @Override
- protected void onProgressUpdate(Void... values) {
-
- super.onProgressUpdate(values);
- }
-
-
- }
第三步,實現一些工具方法。 多線程
over.
Android 之 遠程圖片獲取和本地緩存
http://blog.csdn.net/xieqibao/article/details/6682128
Android應用自動更新功能的代碼實現
http://blog.csdn.net/coolszy/article/details/7518345
Android實戰技巧:多線程AsyncTask
http://blog.csdn.net/hitlion2008/article/details/7560878
Android實戰技巧:消息循環與Looper
http://blog.csdn.net/hitlion2008/article/details/7561190app