Android and JavaScript call each other

Source: Internet
Author: User

HTML page and JavaCodeThe combination method is generally used when the interface is often changed. You can say that HTML is placed in the network, and the software will access the network to obtain the latest interface as soon as it is opened. The disadvantage is that the access speed is slow due to the impact of network signals.

1. Use webview to display HTML code

2. Allow webview to execute JavaScript

Webview. getsettings (). setjavascriptenabled (true );

3. You can obtain HTML files from the network.

Webview. loadurl ("file: // android_asset/index.html"); // The HTML file is stored in the assets folder.

4. Add an object so that JS can access the method of this object. This object can also call the method in Js.

Webview. addjavascriptinterface (new contact (), "Contact ");

 

The complete sample code is as follows:

:

Mainactivity Import Android. App. activity;
Import Android. content. intent;
Import Android.net. Uri;
Import Android. OS. Bundle;
Import Android. WebKit. webview;

Public ClassMainactivityExtendsActivity {
PrivateWebview;

Public Void Oncreate (bundle savedinstancestate ){
Super . Oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
// Load page
Webview = (webview) findviewbyid (R. Id. webview );
// Javascript execution allowed
Webview. getsettings (). setjavascriptenabled ( True );
// Find HTML files, or use files on the network
Webview. loadurl ("file: // android_asset/index.html ");
// Add an object so that JS can access this object. This object can call methods in Js.
Webview. addjavascriptinterface ( New Contact (), "Contact ");
}

Private Final ClassContact {
//Javascript calls this method to call
Public VoidCall (string phone ){
Startactivity (NewIntent (intent. action_call, Uri. parse ("Tel:" + phone )));
}

// HTML calls this method to pass data
Public Void Showcontacts (){
String JSON = "[{\" Name \ ": \" zxx \ ", \" amount \ ": \" 9999999 \ ", \" phone \": \ "18600012345 \"}] ";
// Call methods in JS
Webview. loadurl ("javascript: Show ('" + JSON + "')");
}
}
} HTML: <! 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; charsets = UTF-8" >
< Title > Insert title here </ Title >
< Script Type = "Text/JavaScript" >
Function Show (jsondata ){
VaR Jsonobjs = Eval (jsondata );
VaR Table = Document. getelementbyid ( " Persontable " );
For ( VaR Y = 0 ; Y < Jsonobjs. length; y ++ ){
VaR Tr = Table. insertrow (table. Rows. Length );
VaR TD1 = Tr. insertcell ( 0 );
VaR Td2 = Tr. insertcell ( 1 );
Td2.align =   " Center " ;
VaR Td3 = Tr. insertcell ( 2 );
Td3.align =   " Center " ;
Td1.innerhtml = Jsonobjs [Y]. Name;
Td2.innerhtml = Jsonobjs [Y]. amount;
Td3.innerhtml =   " <A href = 'javascript: contact. Call (\" " + Jsonobjs [Y]. Phone +   " \ ") '> " + Jsonobjs [Y]. Phone +   " </A> " ;
}
}
</ Script >
</ Head >
< Body Onload = "Javascript: contact. showcontacts ()" >
< Table Border = "0" Width = "100%" ID = "Persontable" Cellspacing = "0" >
< Tr >
< TD Width = "30%" > Name </ TD >
< TD Width = "30%" Align = "Center" > Deposit</ TD >
< TD Align = "Center" > Phone number </ TD >
</ Tr >
</ Table >
</ Body >
</ Html >

To make a call, you must add the following permissions:

<Uses-Permission Android: Name = "android. Permission. call_phone"/>

URL: http://greatverve.cnblogs.com/archive/2012/01/18/android-javascript.html

 

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.