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

方法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. 實現(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 連接進行輸入,則將 DoInput 標志設置為 true;如果不打算使用,則設置為 false。默認值為 true。
conn.setDoOutput(true);// 如果打算使用
URL 連接進行輸出,則將 DoOutput 標志設置為 true;如果不打算使用,則設置為 false。默認值為 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);
}
});
//要實現(xiàn)WebView文件下載,實現(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結尾的文件路徑
new DownLoadThread(url, MainActivity.this).start();
}
}
});
}
}方法2:通過系統(tǒng)自身下載方式下載(會在通知欄顯示下載進度條)
只需要把這個方法改寫如下:
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實現(xiàn)文件下載功能,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享標題:Android中怎么利用WebView實現(xiàn)文件下載功能-創(chuàng)新互聯(lián)
本文來源:http://chinadenli.net/article8/peoop.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供微信公眾號、網站營銷、網站維護、虛擬主機、網站設計、App設計
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)