WebView的使用
来源:互联网 发布:c语言课程设计单词统计 编辑:程序博客网 时间:2024/06/06 14:08
package com.linghang.yunmeng.base.web;import android.annotation.SuppressLint;import android.app.ActionBar;import android.app.ProgressDialog;import android.content.Context;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.os.PersistableBundle;import android.support.annotation.NonNull;import android.support.v7.app.AppCompatActivity;import android.text.TextUtils;import android.webkit.WebSettings;import android.webkit.WebView;import android.webkit.WebViewClient;public class WebViewActivity extends AppCompatActivity{ static WebViewActivity activity; ProgressDialog progressDialog; private WebView webView; @Override public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) { super.onCreate(savedInstanceState, persistentState); activity = this; String url = getIntent().getStringExtra("url"); if (url.startsWith("http")) { startWebView(url); } else { //从assets读 startWebViewFromAsset(url); } webView.getTitle(); } @Override protected void onDestroy() { super.onDestroy(); activity = null; } @SuppressLint({"SetJavaScriptEnabled", "JavascriptInterface"}) private void startWebView(String url) { if (TextUtils.isEmpty(url)) { return; } if (progressDialog == null) { // in standard case YourActivity.this progressDialog = new ProgressDialog(WebViewActivity.this); progressDialog.setMessage("Loading..."); progressDialog.show(); } webView.setWebViewClient(new WebViewClient() { //不跳转到外部浏览器 public boolean shouldOverrideUrlLoading(WebView view, String url) { if (url.startsWith("tel:")) { Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url)); startActivity(intent); } else { view.loadUrl(url); } return true; } public void onLoadResource(WebView view, String url) { } public void onPageFinished(WebView view, String url) { try { if (progressDialog.isShowing()) { progressDialog.dismiss(); progressDialog = null; } } catch (Exception exception) { exception.printStackTrace(); } } }); // Javascript inabled on webview webView.getSettings().setJavaScriptEnabled(true);// webView.getSettings().setJavaScriptEnabled(true); //支持js// webView.getSettings().setUseWideViewPort(false); //将图片调整到适合webview的大小// webView.getSettings().setSupportZoom(true); //支持缩放// webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN); //支持内容重新布局// webView.getSettings().supportMultipleWindows(); //多窗口// webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //关闭webview中缓存 webView.getSettings().setAllowFileAccess(true); //设置可以访问文件// webView.getSettings().setNeedInitialFocus(true); //当webview调用requestFocus时为webview设置节点// webView.getSettings().setBuiltInZoomControls(true); //设置支持缩放// webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口// webView.getSettings().setLoadWithOverviewMode(true); // 缩放至屏幕的大小 webView.getSettings().setLoadsImagesAutomatically(true); //支持自动加载图片 webView.getSettings().setAppCacheEnabled(true);// webView.getSettings().setAppCachePath(FILE_PATH.cache);// webView.setWebChromeClient(// new InjectedChromeClient("Model", HostJsScope.class) {// @Override// public void onReceivedTitle(WebView view, String title) {// super.onReceivedTitle(view, title);//// if (isEmpty(titleView.getText().toString())) {// titleView.setText(title);// }//// }// }// ); /* String summary = "<html><body>You scored <b>192</b> points.</body></html>"; webview.loadData(summary, "text/html", null); */ //Load url in webview webView.loadUrl(url);// webView.loadUrl("file:///android_asset/html/agreement.html"); }// public static class HostJsScope {// public static void getIDsMsg(WebView webView, final int gid, final int pid) {// Log.d(AppConst.TAG, String.format("enterGoodDetail: %d %d", gid, pid));// if (activity != null) {// activity.startActivity(GoodDetailActivity.getCallingIntent(// activity, false, "" + gid, "" + pid));// }// }// } private void startWebViewFromAsset(String filename) { WebSettings webSettings = webView.getSettings(); webSettings.setLoadWithOverviewMode(true); webSettings.setUseWideViewPort(true); webView.loadUrl("file:///android_asset/html/" + filename); } @Override public void onBackPressed() { if (webView.canGoBack()) { webView.goBack(); } else { super.onBackPressed(); } }}
加载HTML代码的时候:
webView.loadData(Html.fromHtml(url).toString(), "text/html", "utf-8"); //此方法会导致乱码webView.loadDataWithBaseURL(null, Html.fromHtml(conent).toString(), "text/html", "utf-8", null);
0 0
- Android WebView的使用
- 浅谈WebView的使用
- WebView的使用
- Android WebView的使用
- WebView的使用
- WebView的使用
- Android-WebView的使用
- 浅谈WebView的使用
- android webview的使用
- android webview.的使用
- android WebView的使用
- WebView的使用
- WebView的使用
- 浅谈WebView的使用
- WebView的使用
- webView的使用
- 简单webview的使用
- 浅谈WebView的使用
- 微服务架构下的数据一致性保证(二):可靠事件模式
- js的基本常识
- 三、OSM数据导入PostGIS数据库的过程
- 使用xib布局UIView
- 【EmbeddedDev】BBB&RTL8188CUS搭建wifi AP 问题整理
- WebView的使用
- 关于字符串和小数的互相转换?
- json的二三事儿
- 解决 IE 不支持 document.getElementsByClassName()
- 无效的表达式项 else
- 深入理解LabVIEW中的While循环
- 棋盘覆盖2(1X2骨牌 和 L型骨牌 混合铺满)(强行轮廓线DP)
- html 图像 链接
- 回文字符串