但凡是做Android開(kāi)發(fā)的相信都對(duì)webview不會(huì)陌生晤斩,而且也對(duì)系統(tǒng)自帶的webview本身存在的問(wèn)題也是怨念很久了,一方面是本身對(duì)js的支持不是很好另外一方面就是經(jīng)常被人詬病的內(nèi)存泄露了昔穴。
不知道各位遇到同樣問(wèn)題的朋友是怎么解決的,網(wǎng)上也有很多解析和方案但至少在我的項(xiàng)目中是沒(méi)任何效果的,今天我就分享一下我最終是怎么解決這些問(wèn)題的(其實(shí)是很蠢的一個(gè)辦法)闺鲸。
需求背景:
需要一個(gè)帶有加載進(jìn)度條的webview來(lái)正常的顯示合作方和自己的web頁(yè)面瞻赶。
1赛糟、解決webview對(duì)一些js的支持:
用JsBridge代替系統(tǒng)原生的webview,
github地址:https://github.com/lzyzsd/JsBridge
2砸逊、解決webview內(nèi)存泄露:
@Bind(R.id.pb)
ProgressBar pb;
@Bind(R.id.mWebView)
BridgeWebView mWebView;
pb.setMax(100);
mWebView.setWebChromeClient(new WebViewClient());
mWebView.loadUrl(strWebsite);
private class WebViewClient extends WebChromeClient {
@Override
public void onProgressChanged(WebView view, int newProgress) {
try {
pb.setProgress(newProgress);
if (newProgress == 100) {
pb.setVisibility(View.GONE);
}
} catch (Exception e) {
e.printStackTrace();
}
super.onProgressChanged(view, newProgress);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
try {
if (mWebView != null) {
mWebView.removeAllViews();
mWebView.destroy();
mWebView = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
最后介紹大家一個(gè)用來(lái)檢測(cè)應(yīng)用內(nèi)存泄露的工具:leakcanary
github地址:https://github.com/square/leakcanary
FullStackEngineer的公眾號(hào)璧南,更多分享