1.activity
package com.sam.more.activitys.webvIEw;import androID.content.Context;import androID.os.Bundle;import androID.os.Handler;import androID.os.Message;import androID.support.v7.app.AppCompatActivity;import androID.vIEw.VIEw;import androID.webkit.DownloadListener;import androID.webkit.WebVIEw;import androID.webkit.WebVIEwClIEnt;import androID.Widget.button;import androID.Widget.EditText;import com.sam.more.R;import butterknife.BindVIEw;import butterknife.ButterKnife;import butterknife.OnClick;public class WebVIEwDownloadActivity extends AppCompatActivity implements DownloadCallback { @BindVIEw(R.ID.webv_download) WebVIEw webvDownload; @BindVIEw(R.ID.et_url) EditText etUrl; @BindVIEw(R.ID.btn_download) button btnDownload; Context context; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_web_vIEw_download); ButterKnife.bind(this); context = this; initVIEw(); } private voID initVIEw() { webvDownload.setWebVIEwClIEnt(new WebVIEwClIEnt() { @OverrIDe public boolean shouldOverrIDeUrlLoading(WebVIEw vIEw, String url) { vIEw.loadUrl(url); return true; } }); webvDownload.setDownloadListener(new DownloadListener() { @OverrIDe public voID onDownloadStart(String url, String userAgent, String contentdisposition, String mimetype, long contentLength) { new Thread(new DownLoadThread(url, WebVIEwDownloadActivity.this)).start(); } }); } @OnClick({R.ID.btn_download}) public voID onClick(VIEw vIEw) { switch (vIEw.getID()) { case R.ID.btn_download: webvDownload.loadUrl(etUrl.getText().toString().trim()); break; } } @OverrIDe public voID percentage(final String percentage) { Message message = new Message(); message.obj = percentage; mHandler.sendMessage(message); } private Handler mHandler = new Handler(new Handler.Callback() { @OverrIDe public boolean handleMessage(Message message) { if (message.obj.toString().equals("100%")) { btnDownload.setText("下载完成"); mHandler.removeCallbacksAndMessages(null); return false; } btnDownload.setText(message.obj.toString()); return true; } });}
2.layout
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" tools:context=".activitys.webvIEw.WebVIEwDownloadActivity"> <EditText androID:ID="@+ID/et_url" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="http://csdn-app.csdn.net/csdn.apk" /> <button androID:ID="@+ID/btn_download" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="下载" /> <WebVIEw androID:ID="@+ID/webv_download" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" /></linearLayout>
3.thread
package com.sam.more.activitys.webvIEw;import androID.os.Environment;import androID.util.Log;import java.io.file;import java.io.fileOutputStream;import java.io.IOException;import java.io.inputStream;import java.net.httpURLConnection;import java.net.URL;import java.text.DecimalFormat;/** * Author: Sam * Date: 2021-03-13 11:32 * Description: */public class DownLoadThread implements Runnable { private String dlUrl; private DownloadCallback callback; DownLoadThread(String dlUrl, DownloadCallback callback) { this.dlUrl = dlUrl; this.callback = callback; } @OverrIDe public voID run() { inputStream in = null; fileOutputStream fout = null; int contentLength, downedfileLength = 0;//文件总大小,已下载大小 DecimalFormat df = new DecimalFormat("0%"); try { URL httpUrl = new URL(dlUrl); httpURLConnection conn = (httpURLConnection) httpUrl.openConnection(); contentLength = conn.getContentLength(); Log.e("文件总大小", contentLength + ""); in = conn.getinputStream(); file downloadfile, sdfile; if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { Log.e("sam", "SD卡可写"); downloadfile = Environment.getExternalStorageDirectory(); sdfile = new file(downloadfile, "csdn_clIEnt.apk"); fout = new fileOutputStream(sdfile); } else { Log.e("sam", "SD卡不存在或者不可读写"); } byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) != -1) { if (fout != null) { fout.write(buffer, 0, len); downedfileLength += len; callback.percentage(df.format((float) downedfileLength / contentLength)); } } } catch (Exception e) { e.printstacktrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printstacktrace(); } } if (fout != null) { try { fout.close(); } catch (IOException e) { e.printstacktrace(); } } } }}
4.callback
package com.sam.more.activitys.webvIEw;/** * Author: Sam * Date: 2021-03-13 13:22 * Description: */public interface DownloadCallback { voID percentage(String percentage);}
到这就已经OK啦,如果对您有一点点帮助,还请给个赞,谢谢!
总结以上是内存溢出为你收集整理的WebView下载文件并显示进度全部内容,希望文章能够帮你解决WebView下载文件并显示进度所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)