Android native WebView JS interaction

Source: Internet
Author: User

Http://www.cnblogs.com/android-blogs/p/4891264.html

HTML pages and Java code in a way commonly used in the context of the interface is often changed, you can speak HTML on the network, the software will be opened to access the network to obtain the latest interface. The disadvantage is that it is affected by the network signal, which results in slow access.

1. Use WebView to display HTML code

2. Allow WebView to execute JavaScript

Webview.getsettings (). Setjavascriptenabled (True);

3. Get an HTML file or get it from the network

Webview.loadurl ("file:///android_asset/index.html"); HTML files are stored in the Assets folder

4. Add an object that allows JS to access the object's methods, which can also invoke the JS method in the object

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 class Mainactivity extends Activity {
Private WebView WebView;

public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Load page
WebView = (WebView) Findviewbyid (R.id.webview);
Allow JavaScript to execute
Webview.getsettings (). Setjavascriptenabled (True);
Locate the HTML file or use the file on the network
Webview.loadurl ("file:///android_asset/index.html");
Add an object that allows JS to access the object's method, which can invoke the method in JS
Webview.addjavascriptinterface (New Contact (), "contact");
}

Private Final class Contact {
JavaScript calls this method to make a phone call
public void call (String phone) {
StartActivity (New Intent (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\ "}]";
Invoking the method 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" >
<meta http-equiv= "Content-type" content= "text/html; Charset=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>
<body onload= "javascript:contact.showcontacts ()" >
<table border= "0" width= "100%" id= "persontable" cellspacing= "0" >
<tr>
&LT;TD width= "30%" > Name </td>
&LT;TD width= "30%" align= "center" > Deposit </td>
&LT;TD align= "center" > Phone </td>
</tr>
</table>
</body>

You need to add permission to make a call:

<uses-permission android:name= "Android.permission.CALL_PHONE"/>

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

Android native WebView JS interaction

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.