Android and JavaScript call each other

Source: Internet
Author: User

The following section describes how Android and JavaScript call each other. In this way, the UI is much simpler to design, and the UI can also be designed to be cross-platform. Now there are a lot of front-end web app frameworks, such as sencha and jquery mobile. We believe that with the development of Web apps in the future, we can also use HTML to design an interface that is as beautiful as a local application. Although these are good, there are still many drawbacks. For example, they are much slower than local framework calls. Because mobile phones are restricted devices, the processing and response are slow, we hope that there will be great development in the future. Haha!

Let's just talk about it. Let's write a webviewdemo to implement mutual calls between Android and JavaScript.

Let's take a look at the controls used in Main. xml:

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "@ string/Hello"/> <webview Android: Id = "@ + ID/webview" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"/> <button Android: Id = "@ + ID/button" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "Call JavaScript"/> </linearlayout>

After that, we provided our demo.html webpage

<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd"> <HTML> 

At last, we attached our core code:

Public class webviewdemoactivity 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. main); webview = (webview) This. findviewbyid (R. id. webview); button = (button) This. findviewbyid (R. id. button); websettings setting = webview. getsettings (); // supports JavaScript setting. setjavascriptenabled (true); // added an interface method for the HTML page to call webview. addjavascriptinterface (new object () {// here I have defined a call method public void startphone (string num) {intent = new intent (); intent. setaction (intent. action_call); intent. setdata (URI. parse ("Tel:" + num); startactivity (intent) ;}}, "Demo"); // load the webview of the page. loadurl ("file: // android_asset/demo.html"); button. setonclicklistener (New onclicklistener () {@ override public void onclick (view v) {// todo auto-generated method stub webview. loadurl ("javascript: Show ('data transmitted by activity ')");}});}}

This project uses a call, so do not forget this Code:

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

The code is relatively simple and annotated, so I will not explain it too much here. Run the project below:

HTML interface is not beautified, so it looks a little ugly. I believe the artist will do better, haha! Next, click the Javascript call button:

We have seen that the activity calls the JavaScript method and transmits parameters to display in the HTML interface. Next we will click the phone number next to James to see what the response is:

OK! We have used HTML to call the activity method and start the system's call number.

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.