Android learning notes: loading HTML pages and mutual calls with JS

Source: Internet
Author: User

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 "";
}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.