Android webview加载网页

Activity:

package com.beini.h5app;

import android.app.Activity;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.GeolocationPermissions;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends Activity {
    private WebView web_view;
    private String url = "file:///android_asset/demo.html";
    private String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        web_view = findViewById(R.id.web_view);
        findViewById(R.id.btn_click).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                web_view.loadUrl("javascript:alertMethod()");
            }
        });
//
        WebSettings webSettings = web_view.getSettings();//浏览器的web设置信息
        webSettings.setJavaScriptEnabled(true);//设置WebView属性,能够执行Javascript脚本
        //设置自适应屏幕,两者合用
        webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小
        //缩放操作
        webSettings.setSupportZoom(true); //支持缩放,默认为true。是下面那个的前提。
        webSettings.setBuiltInZoomControls(true); //设置内置的缩放控件。若为false,则该WebView不可缩放
        webSettings.setDisplayZoomControls(false); //隐藏原生的缩放控件
//      webSettings.setUseWideViewPort(true);// 当容器超过页面大小时,是否放大页面大小到容器宽度
//      webSettings.setLoadWithOverviewMode(true);//当页面超过容器大小时,是否缩小页面尺寸到页面宽度

        //其他细节操作
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //关闭webview中缓存
        webSettings.setAllowFileAccess(true); //设置可以访问文件
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
        webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片
        webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式
        //设置WebView缓存
        //优先使用缓存:
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        //缓存模式如下:
        //LOAD_CACHE_ONLY: 不使用网络,只读取本地缓存数据
        //LOAD_DEFAULT: (默认)根据cache-control决定是否从网络上取数据。
        //LOAD_NO_CACHE: 不使用缓存,只从网络获取数据.
        //LOAD_CACHE_ELSE_NETWORK,只要本地有,无论是否过期,或者no-cache,都使用缓存中的数据。
//        if (NetStatusUtil.isConnected(getApplicationContext())) {
//            webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);//根据cache-control决定是否从网络上取数据。
//        } else {
//            webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);//没网,则从本地获取,即离线加载
//        }
        //每个 Application 只调用一次 WebSettings.setAppCachePath(),WebSettings.setAppCacheMaxSize()
        webSettings.setDomStorageEnabled(true); // 开启 DOM storage API 功能 设置是否启用app缓存
        webSettings.setDatabaseEnabled(true);   //开启 database storage API 功能
        webSettings.setAppCacheEnabled(true);//开启 Application Caches 功能
//        webSettings.setAllowFileAccess(true);//设置是否允许访问文件,例如WebView访问sd卡的文件。不过assets与res文件不受此限制,仍然可以通过“file:///android_asset”和“file:///android_res”访问。
//        webSettings.setDatabaseEnabled(true); // 设置是否启用数据库
//        String cacheDirPath = getFilesDir().getAbsolutePath() + APP_CACAHE_DIRNAME;
//        webSettings.setAppCachePath(cacheDirPath); //设置  Application Caches 缓存目录


        web_view.setWebChromeClient(new MWebChromCLient());//设置浏览器的交互事件
        web_view.setWebViewClient(new MWebViewClient());//设置浏览器的加载事件
        web_view.addJavascriptInterface(this, "beini");//添加本地的js代码接口
//      web_view.setDownloadListener(downloadListener);//设置文件下载监听
        web_view.loadUrl(url);
//      web_view.loadData();// 加载文本数据。第二个参数表示媒体类型,如"text/html";第三个参数表示数据的编码格式,"base64"表示采用base64编码,其余值(包括null)表示url编码。
//        web_view.canGoBack();// 表示返回WebView的上一页
//        web_view.goBack();//返回到上一个页面
    }

    class MWebViewClient extends WebViewClient {//避免打开系统浏览器

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {//页面开始加载。一般在此弹出进度对话框ProgressFialog
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {//页面加载结束。一般在此关闭进度对话框
            super.onPageFinished(view, url);
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {//收到ssl错误
            super.onReceivedSslError(view, handler, error);
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {//收到错误信息
            Log.d(TAG, "         onReceivedError    ");
            super.onReceivedError(view, request, error);
        }

        /**
         * 当按下某个连接时WebViewClient会调用这个方法,并传递参数
         *
         * @param view
         * @param request
         * @return
         */


        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            Log.d(TAG, "        shouldOverrideUrlLoading ");
            view.loadUrl(url);
            return true;
        }

        /**
         * shouldInterceptRequest这个回调可以通知主程序WebView处理的资源(css,js,image等)请求,并允许主程序进行处理后返回数据。如果主程序返回的数据为null,WebView会自行请求网络加载资源,否则使用主程序提供的数据。注意这个回调发生在非UI线程中,所以进行UI系统相关的操作是不可以的。
         *
         * @param view
         * @param request
         * @return
         */
        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
            WebResourceResponse response = null;
            if (url.contains("logo")) {
                try {
                    InputStream localCopy = getApplicationContext().getAssets().open("droidyue.png");
                    response = new WebResourceResponse("image/png", "UTF-8", localCopy);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return response;
        }
    }

    class MWebChromCLient extends WebChromeClient {
        @Override
        public void onReceivedTitle(WebView view, String title) {// 收到页面标题
            super.onReceivedTitle(view, title);
        }

        @Override
        public void onProgressChanged(WebView view, int newProgress) {//页面加载进度发生变化
            super.onProgressChanged(view, newProgress);
            Log.d(TAG, "                onProgressChanged  newProgress=" + newProgress);
        }

        @Override
        public boolean onJsAlert(WebView view, String url, String message, JsResult result) {//弹出js警告框
            Log.d(TAG, "                onJsAlert  ");
            return super.onJsAlert(view, url, message, result);
        }

        @Override
        public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {// 弹出js提示框
            Log.d(TAG, "                onJsPrompt  ");
            return super.onJsPrompt(view, url, message, defaultValue, result);
        }

        @Override
        public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {//弹出js确认框
            Log.d(TAG, "                onJsConfirm  ");
            return super.onJsConfirm(view, url, message, result);
        }

        @Override
        public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {// 网页请求定位权限。通常重写该方法弹出一个确认对话框,提示用户是否允许网页获得定位权限。下面代码表示允许定位权限:
            super.onGeolocationPermissionsShowPrompt(origin, callback);
//            callback.invoke(origin, true, false);//表示运行定位
        }
    }


    @android.webkit.JavascriptInterface
    public void actionFromJs() {
        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(MainActivity.this, "js调用了Native函数", Toast.LENGTH_SHORT).show();
//                String text = logTextView.getText() + "\njs调用了Native函数";
//                logTextView.setText(text);
            }
        });
    }

    @android.webkit.JavascriptInterface
    public void actionFromJsWithParam(final String str) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(MainActivity.this, "js调用了Native函数传递参数:" + str, Toast.LENGTH_SHORT).show();
//                String text = logTextView.getText() +  "\njs调用了Native函数传递参数:" + str;
//                logTextView.setText(text);
            }
        });
    }
}

 

布局:

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn_click"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="执行js方法" />

    <WebView
        android:id="@+id/web_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</LinearLayout>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值