WebView equivalent to the browser in Android, based on WebKit development, you can browse Web files, support CSS Javas cript and HTML
1 webview.getsettings (). setjavascriptenabled (true); // Allow JS to execute
2 settings.setjavascriptcanopenwindowsautomatically (true);//Allow JS to execute
3 Webview.addjavascriptinterface (object, "name"); // adds an object of the Name= "name" to objects. Object If this is the Window.name
The HTML file that loads this machine is as follows:
file:///android_asset/teste.html loading a file under Project assets teste.html
file:///sdcard/index.html loading the index.html file under SDcard
1 webview.loadurl ("file:///android_asset/index.html"); // Note that the location of this resource is under a folder that is tied to Res.
If we do not want to create a new WebView process when using WebView, let the program jump to jump to the following settings
1 webv.setwebviewclient (new2public boolean Shouldoverrideurlloading (WebView view, String URL) {3 view.loadurl (URL); Click on the hyperlink to re-load the original process URL4 returntrue; 5 }6 });
Calling Java methods in JavaScript
1. First bind a current Java object to a JavaScript, using the following method
1 webview.addjavascriptinterface (This, "Js2java"); // This is the current object, bound to the JS something above, the main Js2java scope is global. Once initialized, it can be run anywhere
1 addjavascriptinterface// The Java objects and methods to be bound in the method are to be run in another thread and cannot be run in the thread that constructs them, which is also the purpose of using handler.
1 // map Java Object "Jsinvokeclass" to a JavaScript object named "Js2java" 2 // a way to invoke Java objects through "Window.js2java" in JavaScript 3 mwebview.addjavascriptinterface (new Jsinvokeclass (), "Js2java");
1 /** Web page JavaScript call interface * */ 2 class Jsinvokeclass {3 Public void Back () {4 activity.finish (); 5 }6 }
1 // JavaScript calls Java Object Example 2 <body onload= "Javascript:window.js2java.back ()" >
2. Define the called Java method
Example:
1 Public class Javajsdemo extends Activity {2 3 4 private WebView WebView;5 6 @Override7 8PublicvoidonCreate (Bundle savedinstancestate) {9 Ten super.oncreate (savedinstancestate); One A Setcontentview (r.layout.main); - -WebView =(WebView) Findviewbyid (R.id.webview); the -Webview.getsettings (). setjavascriptenabled (true); - -Webview.addjavascriptinterface ( This, "Js2java"); + -Webview.loadurl ("file:///android_asset/index.html"); + A } at -Publicvoidprintmsg (String msg) { - -System.out.println ("----------msg:" +msg); - - } in -Publicvoidclickdowork () { to +Webview.loadurl ("Javascript:dosomework ()"); - the } * $}
HTML code:
12 34 5<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>6 7<title></title>8 9<script language= "javascript" type= "Html/text" >Ten One functiondosomework () { Adocument.getElementById ("Helloweb"). Innerhtml= "Hellowebview"; - - } the -</script> - - + -<body onload= "JAVASCRIPT:WINDOW.JS2JAVA.PRINTMSG (' Hellowebview ')" > + A<div id= "Hellowebview" > at -<a onclick= "Window.js2java.clickDoWork" > - -<br> -Click me! -</a> in -</div> to +</body> - the1 Public class Javajswebviewdemo extends Activity {2 private WebView Mwebview; 3Private Handler Mhandler =NewHandler (); 4 5PublicvoidonCreate (Bundle icicle) {6 super.oncreate (icicle); 7 Setcontentview (R.layout.webviewdemo); 8Mwebview =(WebView) Findviewbyid (R.id.webview); 9WebSettings websettings =mwebview.getsettings (); TenWebsettings.setjavascriptenabled (true); OneMwebview.addjavascriptinterface (NewObject () { APublicvoidclickdowork () { -Mhandler.post (NewRunnable () { -Publicvoidrun () { theMwebview.loadurl ("Javascript:dosomework ()"); - } - }); - },
Public void printmsg (String msg) { System.out.println ("----------msg:" + msg); }
"Js2java"
mwebview.loadurl ("file:///android_asset/demo.html"22 }
Attention:
Using Addjavascriptinterface under Android 4.2 is a security breach, this article can be used http://www.pedant.cn/2014/07/04/ webview-js-java-interface-research/principle, to avoid the problem of this loophole, is completely JS level of the analytic call, and packaging more comprehensive, more thorough analysis!
Android JS calls each other