WebView的使用

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值