在作美图欣赏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