Android and JavaScript call each other

Source: Internet
Author: User

Android and JavaScript call each other

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

[Java]
  1. Import android. app. Activity;
  2. Import android. content. Intent;
  3. Import android.net. Uri;
  4. Import android. OS. Bundle;
  5. Import android. webkit. WebView;
  6.  
  7. Public class MainActivity extends Activity {
  8. Private WebView webView;
  9.  
  10. Public void onCreate (Bundle savedInstanceState ){
  11. Super. onCreate (savedInstanceState );
  12. SetContentView (R. layout. main );
  13. // Load the page
  14. WebView = (WebView) findViewById (R. id. webView );
  15. // Allows JavaScript Execution
  16. WebView. getSettings (). setJavaScriptEnabled (true );
  17. // Find the Html file or use the file on the network
  18. WebView. loadUrl (file: // android_asset/index.html );
  19. // Add an object so that JS can access the method of the object. The object can call the method in JS.
  20. WebView. addJavascriptInterface (new Contact (), contact );
  21. }
  22.  
  23. Private final class Contact {
  24. // Call this method using JavaScript
  25. Public void call (String phone ){
  26. StartActivity (new Intent (Intent. ACTION_CALL, Uri. parse (tel: + phone )));
  27. }
  28.  
  29. // Html calls this method to pass data
  30. Public void showcontacts (){
  31. String json = [{ame: zxx, amount: 9999999, phone: 8600012345}];
  32. // Call methods in JS
  33. WebView. loadUrl (javascript: show ('+ json + '));
  34. }
  35. }
  36. }

     

    HTML:

    [Html]View plaincopy
    Name Deposit Phone number
    1.  
    2.  
    3.  
    4.  
    5.  
    6. <Script type = text/javascript>
    7. Function show (jsondata ){
    8. Var jsonobjs = eval (jsondata );
    9. Var table = document. getElementById (personTable );
    10. For (var y = 0; y Var tr = table. insertRow (table. rows. length );
    11. Var td1 = tr. insertCell (0 );
    12. Var td2 = tr. insertCell (1 );
    13. Td2.align = center;
    14. Var td3 = tr. insertCell (2 );
    15. Td3.align = center;
    16. Td1.innerHTML = jsonobjs [y]. name;
    17. Td2.innerHTML = jsonobjs [y]. amount;
    18. Td3.innerHTML = + jsonobjs [y]. phone +;
    19. }
    20. }
    21. </Script>
    22.  
    23.  
    24.  
    25.  
    26.  
    27.  
    28.  
    29.  
    30.  
  37.  

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

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.