Android中怎么利用WebView實(shí)現(xiàn)文件下載功能-創(chuàng)新互聯(lián)

本篇文章給大家分享的是有關(guān)Android中怎么利用WebView實(shí)現(xiàn)文件下載功能,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。

創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比岳西網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式岳西網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋岳西地區(qū)。費(fèi)用合理售后完善,10多年實(shí)體公司更值得信賴。

方法1,自定義下載操作

1. 先來(lái)布局

<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)這個(gè)監(jiān)聽(tīng)就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:通過(guò)系統(tǒng)自身下載方式下載(會(huì)在通知欄顯示下載進(jìn)度條)

只需要把這個(gè)方法改寫如下:

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)文件下載功能,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

分享標(biāo)題:Android中怎么利用WebView實(shí)現(xiàn)文件下載功能-創(chuàng)新互聯(lián)
本文來(lái)源:http://muchs.cn/article8/peoop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)、網(wǎng)站營(yíng)銷、網(wǎng)站維護(hù)、虛擬主機(jī)網(wǎng)站設(shè)計(jì)、App設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

小程序開(kāi)發(fā)