protected WebView mWebView;
private FastWebChromeClient mChromeClient;
@Override
protected void initLayouts() {
// setContentView(R.layout.activity_webview_root);
/**
* 降低资源占用,避免Context泄露导致Activity无法释放资源
*/
mWebView = new FreeWebView(getApplicationContext());
initWebView(mWebView);
setContentView(mWebView);
startLoading();
}
@Override
protected void onDestroy() {
/**
* 解决退出Activity后缩放按钮导致的崩溃
*/
mWebView.stopLoading();
ViewGroup parent = (ViewGroup) mWebView.getParent();
parent.removeView(mWebView);
mChromeClient.unRegisterProgressListener();
mWebView.setVisibility(View.GONE);
mWebView.clearCache(true);
mWebView.removeAllViews();
mWebView.destroy();
mWebView = null;
mChromeClient = null;
super.onDestroy();
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressWarnings("deprecation")
@SuppressLint("SetJavaScriptEnabled")
private void initWebView(WebView webView) {
WebSettings webSettings = webView.getSettings();
// 设置WebView属性,能够执行Javascript脚本
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setPluginState(PluginState.ON);
webSettings.setUseWideViewPort(false);
// 设置可以访问文件
webSettings.setAllowFileAccess(true);
webSettings.setSupportMultipleWindows(true);
webSettings.setLoadWithOverviewMode(true);
if(Build.VERSION.SDK_INT >= 19) {
webSettings.setLoadsImagesAutomatically(true);
} else {
webSettings.setLoadsImagesAutomatically(false);
}
webSettings.setDomStorageEnabled(true);
webSettings.setRenderPriority(RenderPriority.HIGH);
webView.setWebViewClient(new MyWebViewClient());
mChromeClient= new FastWebChromeClient();
mChromeClient.registerProgressListener(this);
webView.setWebChromeClient(mChromeClient);
}
/*
* (non-Javadoc)
* @user Zheng
* @date 2015年10月23日
* @time 下午3:21:39
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView != null
&& mWebView.canGoBack()) {
WebBackForwardList mWebBackForwardList = mWebView.copyBackForwardList();
if( mWebBackForwardList.getSize()==1){
finish();
}else if(mWebBackForwardList.getSize()>1){
mWebView.goBack();
}
return true;
}
return super.onKeyDown(keyCode, event);
}
// Web视图
private static class MyWebViewClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if(!view.getSettings().getLoadsImagesAutomatically()) {
view.getSettings().setLoadsImagesAutomatically(true);
}
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler,
SslError error) {
handler.proceed();
}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
}
@Override
public void onReceivedHttpAuthRequest(WebView view,
HttpAuthHandler handler, String host, String realm) {
}
}
@Override
public void progressAlmostEnd(int newprogress) {
stopLoading();
}
}
public class FastWebChromeClient extends WebChromeClient{
/**
* 进度监听
*/
private ProgressChangedNotify mProgressChangedNotify;
/**
* @user Zheng
* @date 2015年10月24日
* @time 下午2:50:58
*/
public void registerProgressListener(ProgressChangedNotify progressListener){
this.mProgressChangedNotify= progressListener;
}
/**
* 是否停止加载动画
*/
private boolean isStopLoading = false;
public interface ProgressChangedNotify{
public void progressAlmostEnd(int newprogress);
}
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
if(mProgressChangedNotify!=null){
if(!isStopLoading){
if(newProgress>90){
mProgressChangedNotify.progressAlmostEnd(newProgress);
isStopLoading = true;
}
}
}
}
public void unRegisterProgressListener(){
mProgressChangedNotify = null;
}
}
本文详细介绍了如何在Android应用中集成并优化BaseWebViewActivity,包括使用FreeWebView替代标准WebView以降低资源占用,实现WebView的生命周期管理,设置WebView属性以支持多种功能,以及实现WebView的前进、后退和页面加载控制。
6682

被折叠的 条评论
为什么被折叠?



