android webview实战

时间:2024-10-26 09:33:14
webSettings = wvShowProduce.getSettings();
//设置WebView属性,能够执行Javascript脚本
webSettings.setJavaScriptEnabled(true);
//设置可以访问文件
webSettings.setAllowFileAccess(true);
//设置支持缩放
webSettings.setBuiltInZoomControls(false);
wvShowProduce.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
webSettings.setCacheMode(webSettings.LOAD_DEFAULT);// 打开缓存 webSettings.setSupportZoom(false);
webSettings.setAllowContentAccess(true); webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true);// 设置此属性,可任意比例缩放
webSettings.setLoadWithOverviewMode(true); // webSettings.setLoadsImagesAutomatically(true);
webSettings.setUseWideViewPort(true);// 将图片调整到适合webview的大小
webSettings.setBlockNetworkImage(false);
wvShowProduce.addJavascriptInterface( (class 的代理名称), "OCModel");
//加载需要显示的网页
wvShowProduce.loadUrl(url); //进行跳转网页
wvShowProduce.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (!TextUtils.isEmpty(url)) {
Log.i("img", url);
Intent intent1 = new Intent(MyShowWebViewActivity.this, MyShowWebViewActivity.class);
intent1.putExtra(Consts.KEY_SENDENTITY2ACTIVITY, url);
startActivity(intent1);
}
return true;
} @Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
rlProgress.setVisibility(View.VISIBLE);
} @Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
rlProgress.setVisibility(View.GONE);
webSettings.setBlockNetworkImage(false); if (!TextUtils.isEmpty(title)) {
if (title.contains("http") || title.contains("您的域名") || title.contains("本地的地址") || title.contains("。。。")) {
title = "";
}
setTitle(title);
} else {
setTitle("");
}
} @Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
rlProgress.setVisibility(View.GONE);
ToastUtil.showShort(MyShowWebViewActivity.this, "出现错误,请稍后再试");
}
});

//获取Title  
wvShowProduce.setWebChromeClient(new WebChromeClient() {
@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
String title_str = title;
if (title.contains("http") || title.contains("") || title.contains("") || title.contains("")) {
title_str = "";
}
MyShowWebViewActivity.this.title = title_str;
setTitle(title);
}
}); //调用js
String call = "javascript:方法名()";
wvShowProduce.loadUrl(call);
//js 调用android
wvShowProduce.addJavascriptInterface(new OCModel(), "OCModel");
 public class OCModel {
// 分享到对话
@JavascriptInterface
public void ShareWithContent(String temp, final String data) {
}