WebView中实现文件功能

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

WebView控制调用相应的WEB页面进行展示。当碰到页面有下载链接的时候,点击上去是一点反应都没有的。原来是因为WebView默认没有开启文件下载的功能,如果要实现文件下载的功能,需要设置WebView的DownloadListener,通过实现自己的DownloadListener来实现文件的下载。具体操作如下:1、设置WebView的DownloadListener:webView.setDownloadListener(newMyWebViewDownLoadListener());2、实现MyWebViewDownLoadListener这个类,具体可以如下这样:1.privateclassMyWebViewDownLoadListenerimplementsDownloadListener{2.3.@Override4.publicvoidonDownloadStart(Stringurl,StringuserAgent,StringcontentDisposition,Stringmimetype,longcontentLength){5.Log.i(tag,url=+url);6.Log.i(tag,userAgent=+userAgent);7.Log.i(tag,contentDisposition=+contentDisposition);8.Log.i(tag,mimetype=+mimetype);9.Log.i(tag,contentLength=+contentLength);10.Uriuri=Uri.parse(url);11.Intentintent=newIntent(Intent.ACTION_VIEW,uri);12.startActivity(intent);13.}14.}这只是调用系统中已经内置的浏览器进行下载,还没有WebView本身进行的文件下载,不过,这也基本上满足我们的应用场景了。我在项目中的运用项目要求这样:1,需要使用WebView加载一个网页;2,网页中有文件下载的链接,点击后需要下载文件到SDcard;3,然后自动打开文件;下面是具体解决办法第一步,对WebView进行一系列设置。1.WebViewwebview=(WebView)layout.findViewById(R.id.webview);2.webview.getSettings().setJavaScriptEnabled(true);3.webview.setWebChromeClient(newMyWebChromeClient());4.webview.requestFocus();5.//webview.loadUrl(file:///android_asset/risktest.html);6.webview.loadUrl(jcrs_sub.get(position).addr);7.//设置web视图客户端8.webview.setWebViewClient(newMyWebViewClient());9.webview.setDownloadListener(newMyWebViewDownLoadListener());10.11.//内部类12.publicclassMyWebViewClientextendsWebViewClient{13.//如果页面中链接,如果希望点击链接继续在当前browser中响应,14.//而不是新开Android的系统browser中响应该链接,必须覆盖webview的WebViewClient对象。15.publicbooleanshouldOverviewUrlLoading(WebViewview,Stringurl){16.L.i(shouldOverviewUrlLoading);17.view.loadUrl(url);18.returntrue;19.}20.21.publicvoidonPageStarted(WebViewview,Stringurl,Bitmapfavicon){22.L.i(onPageStarted);23.showProgress();24.}25.26.publicvoidonPageFinished(WebViewview,Stringurl){27.L.i(onPageFinished);28.closeProgress();29.}30.31.publicvoidonReceivedError(WebViewview,interrorCode,32.Stringdescription,StringfailingUrl){33.L.i(onReceivedError);34.closeProgress();35.}36.}37.38.//如果不做任何处理,浏览网页,点击系统“Back”键,整个Browser会调用finish()而结束自身,39.//如果希望浏览的网页回退而不是推出浏览器,需要在当前Activity中处理并消费掉该Back事件。40.publicbooleanonKeyDown(intkeyCode,KeyEventevent){41.//if((keyCode==KeyEvent.KEYCODE_BACK)&&webview.canGoBack()){42.//webview.goBack();43.//returntrue;44.//}45.returnfalse;46.}第二步,起线程开始下载文件。1.//内部类2.privateclassMyWebViewDownLoadListenerimplementsDownloadListener{3.@Override4.publicvoidonDownloadStart(Stringurl,StringuserAgent,StringcontentDisposition,Stringmimetype,longcontentLength){5.if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){6.Toastt=Toast.makeText(mContext,需要SD卡。,Toast.LENGTH_LONG);7.t.setGravity(Gravity.CENTER,0,0);8.t.show();9.return;10.}11.DownloaderTasktask=newDownloaderTask();12.task.execute(url);13.}14.}15.//内部类16.privateclassDownloaderTaskextendsAsyncTaskString,Void,String{17.publicDownloaderTask(){18.}19.20.@Override21.protectedStringdoInBackground(String...params){22.//TODOAuto-generatedmethodstub23.Stringurl=params[0];24.//Log.i(tag,url=+url);25.StringfileName=url.substring(url.lastIndexOf(/)+1);26.fileName=URLDecoder.decode(fileName);27.Log.i(tag,fileName=+fileName);28.Filedirectory=Environment.getExternalStorageDirectory();29.Filefile=newFile(directory,fileName);30.if(file.exists()){31.Log.i(tag,Thefilehasalreadyexists.);32.returnfileName;33.}34.try{35.HttpClientclient=newDefaultHttpClient();36.//client.getParams().setIntParameter(http.socket.timeout,3000);//设置超时37.HttpGetget=newHttpGet(url);38.HttpResponseresponse=client.execute(get);39.if(HttpStatus.SC_OK==response.getStatusLine().getStatusCode()){40.HttpEntityentity=response.getEntity();41.InputStreaminput=entity.getContent();42.writeToSDCard(fileName,input);43.input.close();44.//entity.consumeContent();45.returnfileName;46.}else{47.returnnull;48.}49.}catch(Exceptione){50.e.printStackTrace();51.returnnull;52.}53.}54.55.@Override56.protectedvoidonCancelled(){57.//TODOAuto-generatedmethodstub58.super.onCancelled();59.}60.61.@Override62.protectedvoidonPostExecute(Stringresult){63.//TODOAuto-generatedmethodstub64.super.onPostExecute(result);65.closeProgressDialog();66.if(result==null){67.Toastt=Toast.makeText(mContext,连接错误!请稍后再试!,Toast.LENGTH_LONG);68.t.setGravity(Gravity.CENTER,0,0);69.t.show();70.return;71.}72.Toastt=Toast.makeText(mContext,已保存到SD卡。,Toast.LENGTH_LONG);73.t.setGravity(Gravity.CENTER,0,0);74.t.show();75.Filedirectory=Environment.getExternalStorageDirectory();76.Filefile=newFile(directory,result);77.Log.i(tag,Path=+file.getAbsolutePath());78.Intentintent=getFileIntent(file);79.startActivity(intent);80.}81.82.@Override83.protectedvoidonPreExecute(){84.//TODOAuto-generatedmethodstub85.super.onPreExecute();86.showProgressDialog();87.}88.89.@Override90.protectedvoidonProgressUpdate(Void...values){91.//TODOAuto-generatedmethodstub92.super.onProgressUpdate(values);93.}94.95.96.}第三步,实现一些工具方法。1.privateProgressDialogmDialog;2.privatevoidshowProgressDialog(){3.if(mDialog==null){4.mDialog=newProgressDialog(mContext);5.

1 / 7
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功