WebView Js调原生,原生调安卓

public class MainActivity extends AppCompatActivity {
    private WebView mWebview;
    private TextView tv_onclick;

    @SuppressLint("AddJavascriptInterface")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item_webview);
        mWebview = (WebView)findViewById(R.id.webview);
        tv_onclick = (TextView) findViewById(R.id.tv_onclick);
        WebSettings webSettings = mWebview.getSettings();
        webSettings.setJavaScriptEnabled(true);//允许使用js
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);//不使用缓存,只从网络获取数据.
        //支持屏幕缩放
        webSettings.setSupportZoom(false);
        webSettings.setBuiltInZoomControls(false);
        //不显示webview缩放按钮
        webSettings.setDisplayZoomControls(true);
        //Android5.0配置Webview加载内容的混合模式
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
        }
        mWebview.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.startsWith("http:") || url.startsWith("https:")) {
                    view.loadUrl(url);
                } //屏蔽掉错误的重定向url:"baidumap://map/?src=webapp.default.all.callnaonopenwebapp?"
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
        mWebview.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {

                }
            }
        });

        // mWebview.loadUrl("https://baijiahao.baidu.com/s?id=1627317227624171773&wfr=spider&for=pc");
        mWebview.loadUrl("file:///android_asset/web.html");
        mWebview.addJavascriptInterface(new JSInterface1(this), "baobao");
        tv_onclick.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void onClick(View v) {
                int a = 2, b = 3;

                //调用H5 Web网页中的Javascript函数add()计算a与b的和。
                mWebview.evaluateJavascript("add(" + a + "," + b + ")", new ValueCallback() {
                    //Javascript返回add()函数的计算结果。
                    @Override
                    public void onReceiveValue(String value) {
                        Toast.makeText(MainActivity.this, ""+value, Toast.LENGTH_SHORT).show();
                    }
                });
                mWebview.loadUrl("javascript:add("+a+","+b+")");

            }
        });

    }
}

public class JSInterface1 {
    private  Context context;
    public JSInterface1(Context context) {
        this.context=context;
    }

    //JavaScript调用此方法
    @JavascriptInterface

    public void callAndroidMethod(int a, float b, String c, boolean d) {
        if (d) {
            String strMessage = "a+b+c=" + a + b + c;
            Toast.makeText(context, "strMessage"+strMessage, Toast.LENGTH_SHORT).show();

        }
    }

}




结果

千万别忘记了把html放在assets中!!!

你可能感兴趣的:(WebView Js调原生,原生调安卓)