Use JavaScript to call Android code
1. Use the addjavascriptinterface method of the webview object
2. the addjavascriptinterface method has two parameters. The first parameter is a class that we generally implement. The class provides the method we want to provide for JavaScript access; the second parameter is the JS object used to access the method declared in OBJ. The call mode is window. interfacename. method Name () or Javascript:
Interfacename. Method Name ();, such as mywebview. addjavascriptinterface (New javascriptinterface (this ),
"Android ");
3. Compile the javascriptinterface class. For example, there is a function named showtoast ().
4. The format for calling in HTML: javascript: Android. showtoast ().
Here is a small example:
Import Android. content. context;
Import Android. widget. Toast;
Public class javascriptinterface {
Private context mcontext;
/** Instantiate the interface and set the context */
Public javascriptinterface (context c ){
Mcontext = C;
}
/** Show a toast from the web page */
Public void showtoast (string toast ){
Toast. maketext (mcontext, toast, Toast. length_short). Show ();
}
}
Import java. Io. bufferedreader;
Import java. Io. file;
Import java. Io. inputstreamreader;
Import Android. App. activity;
Import Android. content. context;
Import Android. OS. Bundle;
Import Android. View. keyevent;
Import Android. WebKit. webview;
Import Android. WebKit. webviewclient;
Public class mainactivity extends activity {
/** Called when the activity is first created .*/
Private webview mywebview;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Mywebview = (webview) findviewbyid (R. Id. mywebview );
Mywebview. getsettings (). setjavascriptenabled (true );
Mywebview. addjavascriptinterface (New javascriptinterface (this ),
"Android ");
String htmltext = getfromassets ("test.html ");
// Load mywebview into HTML
Mywebview. loaddata (htmltext, "text/html", "UTF-8 ");
Mywebview. setwebviewclient (New mywebviewclient ());
}
// This button listens for the Return key, which can be returned to the previous webpage (through hostlistery of the webpage)
Public Boolean onkeydown (INT keycode, keyevent event ){
If (keycode = keyevent. keycode_back) & mywebview. cangoback ()){
Mywebview. Goback ();
Return true;
}
Return super. onkeydown (keycode, event );
}
Public String getfromassets (string filename ){
Try {
Inputstreamreader inputreader = new inputstreamreader (
Getresources (). getassets (). Open (filename ));
Bufferedreader bufreader = new bufferedreader (inputreader );
String line = "";
String result = "";
While (line = bufreader. Readline ())! = NULL)
Result + = line;
If (bufreader! = NULL)
Bufreader. Close ();
If (inputreader! = NULL)
Inputreader. Close ();
Return result;
} Catch (exception e ){
E. printstacktrace ();
}
Return NULL;
}
Class mywebviewclient extends webviewclient {
@ Override
Public Boolean shouldoverrideurlloading (webview view, string URL ){
// Todo auto-generated method stub
View. loadurl (URL );
Return true;
}
}
}
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml" XML: lang = "ZH-CN" dir = "LTR">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<SCRIPT type = "text/JavaScript">
Function showandroidtoast (toast ){
Javascript: Android. showtoast (toast );
}
</SCRIPT>
</Head>
<Body>
<Input type = "button" value = "Say hello"
Onclick = "showandroidtoast ('Hello android! ') "/>
</Body>
</Html>
In Android applications, JavaScript code in webview can be directly called:Import java. Io. bufferedreader;
Import java. Io. file;
Import java. Io. inputstreamreader;
Import Android. App. activity;
Import Android. content. context;
Import Android. content. intent;
Import android.net. Uri;
Import Android. OS. Bundle;
Import Android. OS. Handler;
Import Android. View. keyevent;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. WebKit. websettings;
Import Android. WebKit. webview;
Import Android. WebKit. webviewclient;
Import Android. widget. Button;
Public class mainactivity02 extends activity {
/** Called when the activity is first created .*/
Private webview;
Private button;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. main2 );
Webview = (webview) This. findviewbyid (R. Id. webview );
Button = (button) This. findviewbyid (R. Id. Button );
Websettings setting = webview. getsettings ();
// Set to support Javascript
Setting. setjavascriptenabled (true );
// Added an interface method for calling HTML pages
Webview. addjavascriptinterface (new object (){
// Here I define a dialing method
Public void startphone (string num ){
Intent intent = new intent ();
Intent. setaction (intent. action_call );
Intent. setdata (URI. parse ("Tel:" + num ));
Startactivity (intent );
}
}, "Demo ");
// Load the page
Webview. loadurl ("file: // android_asset/test2.html ");
Button. setonclicklistener (New onclicklistener (){
@ Override
Public void onclick (view v ){
// Todo auto-generated method stub
Webview. loadurl ("javascript: Show ('data Uploaded By activity')"); // call the JavaScript function
/*
* The Name Of The current webpage can be called through webview. loadurl ("javascript: XXX ").
* JavaScript method for XXX
*/
}
});
}
}
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> insert title here </title>
<SCRIPT type = "text/JavaScript">
Function show (content ){
Document. getelementbyid ("countent"). innerhtml =
"This is my JavaScript call. This is:" + content;
}
</SCRIPT>
</Head>
<Body>
<Table align = "center">
<Tr> <TD> name </TD> <TD> phone number </TD> </tr>
<Tr> <TD> James </TD> <a href = "javascript: demo. startphone (123) "> 123 </a> </TD> </tr>
<Tr> <TD> JOHN </TD> <a href = "javascript: demo. startphone (456) "> 456 </a> </TD> </tr>
</Table>
<P id = "countent"> HTML raw data </P>
</Body>
</Html>