WebView = (WebView) This. Findviewbyid (R.id.webview); WebSettings websettings=webview.getsettings (); Websettings.setjavascriptenabled (true); Webview.addjavascriptinterface (NewObject () {/*** This annotation, is 4.0, others can call the local method casually. Lack of security, * above 4.0, in order to prevent others from calling other methods. Plus it works. * Android4.0 must be added later to invoke *@paramSTR *@paramParentorigin*/@JavascriptInterface Public voidpostMessage (String str, string parentorigin) {Toastutils.showtoast (Getapplicationcontext (), str); } }, "Parent"); Webview.setscrollbarstyle (View.scrollbars_outside_overlay); //Overwrite WebView default use of third-party or system default browser to open Web page behavior, so that the Web page opened with WebViewWebview.setwebviewclient (Newwebviewclient () {@Override Public Booleanshouldoverrideurlloading (WebView view, String URL) {//The return value is true when the control goes to WebView open, and the system browser or third-party browser is called falseview.loadurl (URL); return true; } });
WebView JS calling Java Local method