The first thing to do is to have JavaScript basics before you can read this article.
(1) JS calls the Android method:
Copy Code code as follows:
WebView Wview;
Wview. Addjavascriptinterface (Object obj, String InterfaceName);
is an instantiation of an object, called in the HTML JS, the second parameter is the alias of the instantiated object, if you want to use this obj, the name used in JS
Is InterfaceName.
Copy Code code as follows:
public class Jswebdemo extends activity {
WebView Wview;
/** Called when the activity is a-created */
@Override
public void OnCreate (Bundle savedinstancestate) {
& nbsp; super.oncreate (savedinstancestate);
Setcontentview (r.layout.main);
Wview = (webview) Findviewbyid (R.ID.WV1);
websettings WSet = wview.getsettings ();
wset.setjavascriptenabled (true);
Proxybridge Pbridge = new Proxybridge ();
Wview.addjavascriptinterface (Pbridge, "Aliansbridge");
Wview.loadurl ("file:///android_asset/index.html");
}
Private class Proxybridge {
public int One () {
return 1;
}
}
}
Take a look at the contents of index.html:
Copy Code code as follows:
<mce:script language= "JavaScript" ><!--
/* This function are invoked by the activity * *
Function Wave () {
Alert ("1");
document.getElementById ("Droid"). src= "Android_waving.png";
Alert ("2");
}
--></mce:script>
<body>
<div id= "Output" >test page.</div>
<a onclick= "window.demo.clickOnAndroid ()" >
<div style= "width:100px;
margin:0px Auto;
padding:10px;
Text-align:center;
border:2px solid #202020; ">
<br>
Click me!
</div>
</a>
<input type= "Submit" value= "Change to 1" onclick= "document.getElementById (' output '). InnerHTML
=aliansbridge.one () "/>
</body>
The Aliansbridge here is the Java object that is invoked.
Note that if you have only one way to be called by JS only a new object will be OK:
Proxybridge Pbridge = new Proxybridge ();
Wview.addjavascriptinterface (Pbridge, "Aliansbridge");
If you need to invoke multiple methods, you must instantiate the entire Android program's class itself:
Wview.addjavascriptinterface (This, "Aliansbridge");
(2) Android calls JS method:Wview.setwebchromeclient (webchromeclient client)
To use the Handler update ui,handler mhandler = new Handler ();
Copy Code code as follows:
Mwebview.setwebchromeclient (New Mywebchromeclient ());
Mwebview.addjavascriptinterface (New Object () {
/**
* This isn't called on the UI thread. Post a runnable to invoke
* Loadurl on the UI thread.
*/
public void Clickonandroid () {
Mhandler.post (New Runnable () {
public void Run () {
Mwebview.loadurl ("Javascript:wave ()");
}
});
}
}, "demo");
Similarly, there must be an object called demo in HTML to invoke the Clickonandroid () method.
Copy Code code as follows:
/**
* Provides a hook for calling ' alert ' from JavaScript. Useful for
* Debugging your JavaScript.
*/
Final class Mywebchromeclient extends Webchromeclient {
@Override
public boolean Onjsalert (WebView view, string URL, String message,
Jsresult result) {
Result.confirm ();
Return Super.onjsalert (view, URL, message, result);
}
}