Main:
<Webview
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: Id = "@ + ID/webview"
/>
Activity:
Public class htmluiactivity extends activity {
Private webview;
Public handler;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Webview = (webview) This. findviewbyid (R. Id. webview );
Webview. getsettings (). setjavascriptenabled (true); // enable Javascript support
Webview. getsettings (). setsuppzoom zoom (false );
Webview. getsettings (). setappcacheenabled (false );
Webview. getsettings (). setallowfileaccess (true );
// Send data to JS in Java
// "Myjs" can be used in JS to call the show method. myjs is equivalent to a myjavascript object.
// You can call methods in myjavascript
Webview. addjavascriptinterface (New myjavascript (this, Handler), "myjs ");
String url = "file: // android_asset/index.html"; // path under assets
Webview. loadurl (URL );
}
}
Communication Class with JS:
Public class myjavascript {
Private context;
Private webview;
Private handler;
Public myjavascript (context, Handler handler ){
This. Context = context;
This. Handler = handler;
Webview = (webview) (activity) Context). findviewbyid (R. Id. webview );
}
// Call the contactlist () method in JS in Java and pass the Parameter
Public void show (){
Handler. Post (New runnable (){
@ Override
Public void run (){
Webview. loadurl ("javascript: contactlist ('" + query () + "')");
}
});
}
// Call (call permission required)
Public void call (final string phone ){
Handler. Post (New runnable (){
@ Override
Public void run (){
Context. startactivity (new intent (intent. action_call, Uri. parse ("Tel:" + phone )));
}
});
}
Public String query (){
Try {
Jsonobject = new jsonobject ();
Jsonobject. Put ("ID", 56 );
Jsonobject. Put ("name", "Hu yidao ");
Jsonobject. Put ("phone", "151--323 ");
Jsonobject jsonobject2 = new jsonobject ();
Jsonobject2.put ("ID", 96 );
Jsonobject2.put ("name", "Hu Yong ");
Jsonobject2.put ("phone", "151--326 ");
Jsonarray = new jsonarray ();
Jsonarray. Put (jsonobject );
Jsonarray. Put (jsonobject2 );
Return jsonarray. tostring ();
} Catch (jsonexception e ){
}
Return "";
}
}