WebView Js 조 원생, 원생 조 안 드 로 이 드

3724 단어
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();

        }
    }

}




function add(x,y){
    sum=x+y;
   document.getElementById("content").innerHTML +=
   "<br>" + sum;
    return sum;
}



html 를 assets 에 넣 는 것 을 잊 지 마 세 요!!

좋은 웹페이지 즐겨찾기