Android and JavaScript call methods, androidjavascript

Source: Internet
Author: User

Android and JavaScript call methods, androidjavascript

Address: http://www.jb51.net/article/77206.htm

This article mainly introduces how Android and JavaScript call each other. The example analyzes Android WebView's skills for executing JavaScript and JavaScript to access Android. For more information, see:

This document describes how Android and JavaScript call each other. We will share this with you for your reference. The details are as follows:

The combination of Html pages and Java code is generally used when the interface is often changed. You can talk about html in the network. Once the software is opened, it will access the network to obtain the latest interface. 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 class MainActivity extends Activity {private WebView webView; public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // load the page webView = (WebView) findViewById (R. id. webView); // allows JavaScript to execute webView. getSettings (). setJavaScriptEnabled (true); // find the Html file, or use the webView file on the network. loadUrl ("file: // android_asset/index.html"); // Add an object so that JS can access the method of this object. This object can call webView in JS. addJavascriptInterface (new Contact (), "contact");} private final class Contact {// JavaScript call this method to call public void call (String phone) {startActivity (new Intent (Intent. ACTION_CALL, Uri. parse ("tel:" + phone);} // Html call this method to pass public void showcontacts () {String json = "[{\" name \": \ "zxx \", \ "amount \": \ "9999999 \", \ "phone \": \ "18600012345 \"}] "; // call the webView method in JS. loadUrl ("javascript: show ('" + json + "')");}}}

HTML:

"-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> 

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

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

I hope this article will help you with Android program design.

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.