android WebView实现java与javascript的交互

来源:互联网 发布:淘宝实拍保护平台 编辑:程序博客网 时间:2024/05/17 13:43

转自:http://ff20081528.iteye.com/blog/1489565


最近在学习html5,网上有很多文章都在分析预测移动互联的未来,很多人的观点是html5会是移动互联的未来,但是不可能完全取代app应用。未来很多的应用将会是html5+native来架构。所以自己想写一个类似的demo,在网上找了好久也没找到一个比较好的文章。为此自己写了一个,主要就是用了WebView类,在此贴出来与大家分享。 

1,demo的结构图 

 

2,main.xml 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <WebView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:id="@+id/webview"/>  
  11.   
  12. </LinearLayout>  


3,index.html 
Html代码  收藏代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5. <title>webview</title>  
  6. <script type="text/javascript">  
  7.     function show(jsondata) {  
  8.     alert("运行" + jsondata);  
  9.         var jsonobjs = eval(jsondata);  
  10.         var table = document.getElementById("personTable");  
  11.         for(var y=0; y<jsonobjs.length; y++){  
  12.             var tr = table.insertRow(table.rows.length);  
  13.             var td1 = tr.insertCell(0);  
  14.             var td2 = tr.insertCell(1);  
  15.             td2.align = "center";  
  16.             var td3 = tr.insertCell(2);  
  17.             td3.align = "center";  
  18.             alert(jsonobjs[y].id);  
  19.             td1.innerHTML = jsonobjs[y].id;  
  20.             td2.innerHTML = jsonobjs[y].name;  
  21.             //td3.innerHTML = jsonobjs[y].mobile;  
  22.             td3.innerHTML = "<a href='javascript:contact.call(\""+jsonobjs[y].mobile+"\")'>" + jsonobjs[y].mobile;  
  23.         }  
  24.     }  
  25.       
  26.       
  27. </script>  
  28. </head>  
  29.   
  30. <body onload="javascript:contact.getContacts()">  
  31.     <table border="0" width="100%" id="personTable" cellpadding="0">  
  32.         <tr>  
  33.             <td width="15%">编号</td>  
  34.             <td width="20%" align="center">姓名</td>  
  35.             <td aligh="center">电话</td>  
  36.         </tr>  
  37.     </table>  
  38.     [url=javascript:window.location.reload()]刷新[/url]  
  39. </body>  
  40. </html>  


4,Contact.java 
Java代码  收藏代码
  1. package org.sunday.demain;  
  2. /** 
  3.  * 实体类 
  4.  * @author sunday 
  5.  * 
  6.  */  
  7. public class Contact {  
  8.     private Integer id;  
  9.     private String name;  
  10.     private String mobile;  
  11.     public Integer getId() {  
  12.         return id;  
  13.     }  
  14.     public void setId(Integer id) {  
  15.         this.id = id;  
  16.     }  
  17.     public String getName() {  
  18.         return name;  
  19.     }  
  20.     public void setName(String name) {  
  21.         this.name = name;  
  22.     }  
  23.     public String getMobile() {  
  24.         return mobile;  
  25.     }  
  26.     public void setMobile(String mobile) {  
  27.         this.mobile = mobile;  
  28.     }  
  29.     public Contact(Integer id, String name, String mobile) {  
  30.         super();  
  31.         this.id = id;  
  32.         this.name = name;  
  33.         this.mobile = mobile;  
  34.     }  
  35. }  

5,ContactService.java 
Java代码  收藏代码
  1. package org.sunday.service;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import org.sunday.demain.Contact;  
  7. /** 
  8.  * 业务逻辑  模拟从服务端获取数据 
  9.  * @author sunday 
  10.  * 
  11.  */  
  12. public class ContactService {  
  13.     public List<Contact> getContacts() {  
  14.         List<Contact> contacts = new ArrayList<Contact>();  
  15.         contacts.add(new Contact(34"张三""15170013856"));  
  16.         contacts.add(new Contact(39"李四""15170013856"));  
  17.         contacts.add(new Contact(67"王五""15170013856"));  
  18.         return contacts;  
  19.     }  
  20. }  

6,MyWebViewActivity.java 
Java代码  收藏代码
  1. package org.sunday.main;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.json.JSONArray;  
  6. import org.json.JSONObject;  
  7. import org.sunday.demain.Contact;  
  8. import org.sunday.service.ContactService;  
  9.   
  10. import android.app.Activity;  
  11. import android.content.Intent;  
  12. import android.net.Uri;  
  13. import android.os.Bundle;  
  14. import android.util.Log;  
  15. import android.webkit.WebView;  
  16. /** 
  17.  *  
  18.  * @author sunday 
  19.  * 
  20.  */  
  21. public class MyWebViewActivity extends Activity {  
  22.     /** Called when the activity is first created. */  
  23.     private static final String TAG = "MyWebViewActivity";  
  24.     private ContactService contactService;  
  25.     private WebView webView;  
  26.     @Override  
  27.     public void onCreate(Bundle savedInstanceState) {  
  28.         super.onCreate(savedInstanceState);  
  29.         setContentView(R.layout.main);  
  30.           
  31.         contactService = new ContactService();  
  32.           
  33.         webView = (WebView) findViewById(R.id.webview);  
  34.         //设置webView可以使用Javascript  
  35.         webView.getSettings().setJavaScriptEnabled(true);  
  36.         //将插件类实例绑定到javascript中  
  37.         webView.addJavascriptInterface(new ContactPlugin(), "contact");  
  38.         //加载html,如果有必要此页面可以放在服务器端  
  39.         webView.loadUrl("file:///android_asset/index.html");  
  40.     }  
  41.       
  42.     //插件类  
  43.     private final class ContactPlugin {  
  44.         //获取联系人  
  45.         public void getContacts() {  
  46.             List<Contact> contacts = contactService.getContacts();  
  47.             try {  
  48.                 JSONArray array = new JSONArray();  
  49.                 for(Contact contact : contacts) {  
  50.                     JSONObject item = new JSONObject();  
  51.                     item.put("id", contact.getId());  
  52.                     item.put("name", contact.getName());  
  53.                     item.put("mobile", contact.getMobile());  
  54.                     array.put(item);  
  55.                 }  
  56.                 String json = array.toString();  
  57.                 Log.e(TAG, "json = " + json);  
  58.                 webView.loadUrl("javascript:show('"+json+"')");  
  59.             } catch (Exception e) {  
  60.                 e.printStackTrace();  
  61.             }  
  62.         }  
  63.         //调用拨号器  
  64.         public void call(String phoneNum) {  
  65.             Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNum));  
  66.             startActivity(intent);  
  67.         }  
  68.     }  
  69.       
  70. }