There are two forms of interaction between Android and js: 1.js call Android method and pass parameters: mWebSettings.setJavaScriptEnabled(true); //Add this sentence to use the javascript method mWebView.addJavascriptInterface(new Object() {//Add interface Method, let the html page call @JavascriptInterface public void jsMethod(String id,String type) { Toast.makeText(MainActivity.this,"parameter 1:"+id+"parameter 2:"+type,Toast.LENGTH_LONG).show() ; } @JavascriptInterface public void jsTitle( String title) { Toast.makeText(MainActivity.this,"Parameter 1:"+title,Toast.LENGTH_LONG).show(); } }, "stub"); 2. Android calls js Method and pass parameters: mWebView. @Override public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error){ //Note: The super sentence must be deleted or commented out, otherwise the handler.cancel() default does not support https. //super.onReceivedSslError(view, handler, error); //handler.cancel(); // Android default processing method //handler.handleMessage(Message msg); // other processing handler.proceed(); / / Accept all website certificates } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // view.loadUrl(url);// When opening a new link, use the current WebView instead of using other browsers in the system to return true; } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); String usr="ppp"; Log.i("=====", "onPageFinished: -------- "); //Execute the js function you want to call here mWebView.loadUrl("javascript:test_coupon('"+usr+ "','" +usr+"')"); //load the page } });
Android and js interaction
Guess you like
Origin blog.csdn.net/u010256329/article/details/62216319
Recommended
Ranking