但凡是做Android開發(fā)的相信都對webview不會陌生,而且也對系統(tǒng)自帶的webview本身存在的問題也是怨念很久了,一方面是本身對js的支持不是很好另外一方面就是經(jīng)常被人詬病的內(nèi)存泄露了。
不知道各位遇到同樣問題的朋友是怎么解決的,網(wǎng)上也有很多解析和方案但至少在我的項目中是沒任何效果的,今天我就分享一下我最終是怎么解決這些問題的(其實是很蠢的一個辦法)。
需求背景:
需要一個帶有加載進(jìn)度條的webview來正常的顯示合作方和自己的web頁面。
1、解決webview對一些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();
}
}
最后介紹大家一個用來檢測應(yīng)用內(nèi)存泄露的工具:leakcanary
github地址:https://github.com/square/leakcanary
FullStackEngineer的公眾號,更多分享