Android和JavaScript互相調用

來源:互聯網
上載者:User

Android和JavaScript互相調用

Html頁面和Java代碼結合的方式一般用在介面經常被更改 的情況下,可以講html放在網路中,軟體一開啟就會訪問網路擷取到最新的介面。缺點是會受到網路訊號的影響,從而導致訪問速度慢。

1.用WebView來顯示HTML代碼

2.允許WebView執行JavaScript

webView.getSettings().setJavaScriptEnabled(true);

3.擷取到HTML檔案,也可從網路中擷取

webView.loadUrl(file:///android_asset/index.html); //HTML檔案存放在assets檔案夾中

4.添加一個對象, 讓JS可以訪問該對象的方法, 該對象中也可以調用JS中的方法

webView.addJavascriptInterface(new Contact(), contact);

 

完整範例程式碼如下:

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. //載入頁面
  14. webView = (WebView) findViewById(R.id.webView);
  15. //允許JavaScript執行
  16. webView.getSettings().setJavaScriptEnabled(true);
  17. //找到Html檔案,也可以用網路上的檔案
  18. webView.loadUrl(file:///android_asset/index.html);
  19. // 添加一個對象, 讓JS可以訪問該對象的方法, 該對象中可以調用JS中的方法
  20. webView.addJavascriptInterface(new Contact(), contact);
  21. }
  22.  
  23. private final class Contact {
  24. //JavaScript調用此方法撥打到電話
  25. public void call(String phone) {
  26. startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(tel: + phone)));
  27. }
  28.  
  29. //Html調用此方法傳遞資料
  30. public void showcontacts() {
  31. String json = [{ ame:zxx, amount:9999999, phone:8600012345}];
  32. // 調用JS中的方法
  33. webView.loadUrl(javascript:show(' + json + '));
  34. }
  35. }
  36. }

     

    HTML:

    [html] view plaincopy
    姓名 存款 電話
    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. 撥打到電話需要添加許可權:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.