Android (H5) method call each other

When mixed record it in front of a very important development java and js intermodulation method for data exchange.

This mixed-use development will need to control the webview

This is metaphysics, ha ha ha

This article can look https://www.jianshu.com/p/3d9a93c9fea2

The first to set about webview. WebSettings configured to manage the state WebView

public  static  Boolean WebViewSetting (the Activity Activity, the WebView Webview) { 

        Final the WebSettings WebSettings = webview.getSettings (); 

        webSettings.setDomStorageEnabled ( to true ); // main sentence 
        webSettings.setJavaScriptEnabled ( to true ); // enable JS 
        webSettings.setBlockNetworkImage ( false ); // solve the picture does not appear 
        webSettings.setSavePassword ( false ); 
        webSettings.setDefaultTextEncodingName ( "UTF-8"); // set the encoding format 
        webSettings.getSettings () setBuiltInZoomControls ().;// set whether to support the scaling 
        webSettings.addJavascriptInterface (obj, str); // inject java object to a html page 
        webSettings.setUseWideViewPort ( to true ); // set this property, can be arbitrary scaling 
        webSettings.setLoadWithOverviewMode ( to true ); // page support zoom: 
        webSettings.setJavaScriptEnabled ( to true ); 
        webSettings.setBuiltInZoomControls ( to true ); 
        webUrl.requestFocusFromTouch (); // if user webView need to manually enter a user name, password, or other, you must set the webview support for gestures focus. 
        webSettings.setJavaScriptEnabled ( to true );   // support js
        webSettings.setUseWideViewPort ( false );   // will adjust the picture size to fit webview 
        webSettings.setSupportZoom ( to true );   // support scaling webSettings.setLayoutAlgorithm (LayoutAlgorithm.SINGLE_COLUMN); // support content re-layout 
        webSettings.supportMultipleWindows ();   / / multi-window 
        webSettings.setCacheMode (WebSettings.LOAD_CACHE_ELSE_NETWORK);   // close webview cache 
        webSettings.setAllowFileAccess ( to true );   // set can access files 
        webSettings.setNeedInitialFocus ( to true ); // when webview webview call requestFocus to set the node
        webSettings.setJavaScriptCanOpenWindowsAutomatically ( to true ); // support open a new window by JS 
        webSettings.setLoadWithOverviewMode ( to true ); // scaled to the size of the screen 
        webSettings.setLoadsImagesAutomatically ( to true );   // support Load images automatically 

        // allow JavaScript can automatically open windows JS settings allow pop 
        webSettings.setJavaScriptCanOpenWindowsAutomatically ( to true );
         // set the cache 
        webSettings.setAppCacheEnabled ( false );
         // set the cache mode, a total of four modes
 //        webSettings.setCacheMode (WebSettings.LOAD_CACHE_ELSE_NETWORK);
         // set the cache path 
        webSettings.setAppCachePath ( "" );
         // support scaling (to fit the current screen) 
        webSettings.setSupportZoom ( to true );
         // will adjust the picture to the appropriate size 
        webSettings .setUseWideViewPort ( to true );
         // support content re-layout, a total of four ways
         // default is NARROW_COLUMNS 
        webSettings.setLayoutAlgorithm (WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
         // set the default font size 
        webSettings.setDefaultFontSize (12 );
         // support zoom
        webSettings.setSupportZoom ( to false );
         // set two support means zoom gesture 
        webSettings.setBuiltInZoomControls ( to false ); 

        return  to true ; 
    }

The following important here

addJavascriptInterface native WebKit the API is, belongs to the public methods WebView object, for exposing a java object to js, ​​js such methods can be called directly.

Because it is not secure, after 4.2 added @JavascriptInterface comment

There is a framework: https: //github.com/lzyzsd/JsBridge --- This project between Java and JavaScript bridge the gap.

Now to begin loading the page a webview.loadUrl ( "https://www.baidu.com/");

@SuppressLint("JavascriptInterface")
    private void initMixedPage() {

        //封装webview
        NativeWebViewUtil nativeWebViewUtil = new NativeWebViewUtil();
        nativeWebViewUtil.WebViewSetting(this,webview);
        //添加Javascript的映射
        webview.addJavascriptInterface(this,"android");
        webview.loadUrl("https://www.baidu.com");
        webview.setWebViewClient(new WebViewClient(){

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                // 加载页面
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                

                // 加载结束
                webview.evaluateJavascript("javascript:get_android_base("aaa")", new ValueCallback<String>() {
                    @Override
                    public void onReceiveValue(String value) {
                        //此处为 js 返回的结果
                        Log.v("Native",value);
                    }
                });
            }
        });

    }

没有回掉可以写null.

第一、Android调用js

有两个很重要的方法setWebChromeClient和setWebClient

setWebChromeClient主要处理解析,渲染网页等浏览器做的事情

WebChromeClient是辅助WebView处理Javascript的对话框等

要调用js就要等webview加载完成后再调用js方法

webview.setWebViewClient(new WebViewClient(){

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                // 加载页面

                
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                
                // 加载结束js 方法 get_android_base
                webview.evaluateJavascript("javascript:get_android_base("aaa")", new ValueCallback<String>() {
                    @Override
                    public void onReceiveValue(String value) {
                        //此处为 js 返回的结果
                        Log.v("Native",value);
                    }
                });
            }
        });

javascript代码

接收android发来的数据

function get_android_base(base){
    alert(base);
}

第二、js调用Android

javascrip代码

js的一个方法 get_data()

window.android.get_data("mcontrol");

 Android代码

@JavascriptInterface
public void get_data(String base){
    Log.d(TAG,base);
}

 到这里,简单的互调就完成了。

互调就很玄学,总是有各种各样的问题哈哈哈。

后面再记录下腾讯的webview

 

Guess you like

Origin www.cnblogs.com/Steffen-xuan/p/11272692.html