本篇文章給大家分享的是有關(guān)Android中怎么利用WebView實(shí)現(xiàn)文件下載功能,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
方法1,自定義下載操作
1. 先來布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:ldm="http://schemas.android.com/apk/res/com.ldm.learn" android:layout_width="match_parent" android:layout_height="match_parent" > <WebView android:id="@+id/test_wv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="15dp" /> </RelativeLayout>
2. 實(shí)現(xiàn)自定義下載工具操作異步線程類:
public class DownLoadThread extends Thread { private String downLoadUrl; private Context context; private FileOutputStream out = null; private File downLoadFile = null; private File sdCardFile = null; private InputStream in = null; public DownLoadThread(String downLoadUrl, Context context) { super(); this.downLoadUrl = downLoadUrl; this.context = context; } @Override public void run() { try { URL httpUrl = new URL(downLoadUrl); HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection(); conn.setDoInput(true);// 如果打算使用 URL 連接進(jìn)行輸入,則將 DoInput 標(biāo)志設(shè)置為 true;如果不打算使用,則設(shè)置為 false。默認(rèn)值為 true。 conn.setDoOutput(true);// 如果打算使用 URL 連接進(jìn)行輸出,則將 DoOutput 標(biāo)志設(shè)置為 true;如果不打算使用,則設(shè)置為 false。默認(rèn)值為 false。 in = conn.getInputStream(); if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { Toast.makeText(context, "SD卡不可用!", Toast.LENGTH_SHORT).show(); return; } downLoadFile = Environment.getExternalStorageDirectory(); sdCardFile = new File(downLoadFile, "download.apk"); out = new FileOutputStream(sdCardFile); byte[] b = new byte[1024]; int len; while ((len = in.read(b)) != -1) { out.write(b, 0, len); } if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (Exception e) { e.printStackTrace(); } } }
3. 文件下載
public class MainActivity extends Activity { private WebView test_wv; private String downLoadUrl = "http://as.baidu.com/a/rank?cid=101&s=1&f=web_alad"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.test_wv = (WebView) findViewById(R.id.test_wv); test_wv.loadUrl(downLoadUrl); test_wv.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return super.shouldOverrideUrlLoading(view, url); } }); //要實(shí)現(xiàn)WebView文件下載,實(shí)現(xiàn)這個監(jiān)聽就ok test_wv.setDownloadListener(new DownloadListener() { @Override public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { Log.v("ldm", url); if (url.endsWith(".apk")) {//判斷是否是.apk結(jié)尾的文件路徑 new DownLoadThread(url, MainActivity.this).start(); } } }); } }
方法2:通過系統(tǒng)自身下載方式下載(會在通知欄顯示下載進(jìn)度條)
只需要把這個方法改寫如下:
test_wv.setDownloadListener(new DownloadListener() { @Override public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { Log.v("ldm", url); Uri uri=Uri.parse(url); Intent intent=new Intent(Intent.ACTION_VIEW, uri); startActivity(intent); } });
以上就是Android中怎么利用WebView實(shí)現(xiàn)文件下載功能,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享標(biāo)題:Android中怎么利用WebView實(shí)現(xiàn)文件下載功能-創(chuàng)新互聯(lián)
本文來源:http://chinadenli.net/article8/peoop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號、網(wǎng)站營銷、網(wǎng)站維護(hù)、虛擬主機(jī)、網(wǎng)站設(shè)計(jì)、App設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容