Calling javascript function from android webview?

后端 未结 2 1618
名媛妹妹
名媛妹妹 2021-01-16 00:23

I try to call a javascript function from directly form my application (webview.apk), in order to start a script which would autoplay a html5 video clip, I have tried to add

相关标签:
2条回答
  • 2021-01-16 00:45

    Kotlin Code to call jsFunction:

    webView.webViewClient = object : WebViewClient() {
        //....
        override fun onPageFinished(view: WebView, url: String) {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
                view.evaluateJavascript("jsFunction();", null)
            } else {
                view.loadUrl("jsFunction();")
                //view.loadUrl("javascript:alert('Hamed');")
            }
        }
        //...
    }
    

    If your function returns a value (json ,..) and you need to capture the result, use this code :

    view.evaluateJavascript("jsFunction();") {
        Log.i("TestJavaScript", "ValueCallBack=$it")
    }
    
    0 讨论(0)
  • 2021-01-16 01:06

    Wait til the page loading is finished and then execute webview.loadUrl("javascript:playVideo()");

    private class MyCustomWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
             view.loadUrl(url);
             return true;
        }
        public void onPageFinished(WebView view, String url) {
             view.loadUrl("javascript:playVideo()");
        }
    }
    
    0 讨论(0)
提交回复
热议问题