android使用html开发软件界面

转载网址:http://blog.csdn.net/crazychickone/article/details/8250879

此处用到的html文件是存放在Android的assets下的资源。路径为:file:///android_asset/index.html

用到的权限有:

   拨打电话权限:
   <uses-permission android:name="android.permission.CALL_PHONE"/>

   允许网络访问权限:
   <uses-permission android:name="android.permission.INTERNET"/>

1.用到的html文件

<!DOCTYPE html>  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
<title>Insert title here</title>  
<script type="text/javascript">  
    function show(jsondata){  
            var jsonobjs = eval(jsondata);//json字符串转换为json对象  
            var table = document.getElementById("personTable");  
            for(var y=0; y<jsonobjs.length; y++){  
                var tr = table.insertRow(table.rows.length); //添加一行  
                //添加三列  
                var td1 = tr.insertCell(0);  
                var td2 = tr.insertCell(1);  
                td2.align = "center";  
                var td3 = tr.insertCell(2);  
                td3.align = "center";  
                //设置列内容和属性  
                td1.innerHTML = jsonobjs[y].id;   
                td2.innerHTML = jsonobjs[y].name;   
                td3.innerHTML = "<a href='javascript:contact.call(\""+ jsonobjs[y].mobile+ "\")'>"+ jsonobjs[y].mobile+ "</a>";   
            }  
    }  
</script>  
  
</head>  
<!-- js代码通过webView调用其插件中的java代码 -->  
<body οnlοad="javascript:contact.getContacts()">  
   <table border="0" width="100%" id="personTable" cellspacing="0">  
        <tr>  
            <td width="20%">编号</td><td width="40%" align="center">姓名</td><td align="center">电话</td>  
        </tr>  
    </table>  
    <a href="javascript:window.location.reload()">刷新</a>  
</body>  
</html>
2. Contact.java

package cn.itcast.domain;

public class Contact {  
    private Integer id;  
    private String name;  
    private String mobile;  
      
    public Contact(Integer id, String name, String mobile) {  
        this.id = id;  
        this.name = name;  
        this.mobile = mobile;  
    }  
    public Integer getId() {  
        return id;  
    }  
    public void setId(Integer id) {  
        this.id = id;  
    }  
    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  
    public String getMobile() {  
        return mobile;  
    }  
    public void setMobile(String mobile) {  
        this.mobile = mobile;  
    }  
}  
2. javaContactService.java

package cn.itcast.service;

import java.util.ArrayList;
import java.util.List;

import cn.itcast.domain.Contact;

public class ContactService {  
    
    public List<Contact> getContacts(){  
        List<Contact> contacts = new ArrayList<Contact>();  
        contacts.add(new Contact(1, "王磊", "5201314"));  
        contacts.add(new Contact(2, "张三", "5201314"));  
        contacts.add(new Contact(3, "李四", "5201314"));  
        return contacts;  
    }  
}  

2. MainActivity .java

package com.nthm.Test;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cn.itcast.domain.Contact;
import cn.itcast.service.ContactService;

import android.net.Uri;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.webkit.WebView;

public class MainActivity extends Activity {  
    private ContactService service;  
    private WebView webView;  
      
    @SuppressLint("SetJavaScriptEnabled")
	@Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
          
        service = new ContactService();  
        webView = (WebView) this.findViewById(R.id.webView);  
        webView.getSettings().setJavaScriptEnabled(true);//允许webkit执行js代码;  
        //对象,插件名称  
        //window.open()  
        //document.write()  
        //contact.xxx  
        //为webkit添加插件,即java类,此处的contact用来在html中调用
        webView.addJavascriptInterface(new ContactPlugin(), "contact");
        webView.loadUrl("file:///android_asset/index.html");//加载html文件  
        //webView.loadUrl("http://192.168.1.100:8080/web/index.html");  
    }  
      
    private final class ContactPlugin{  
        public void getContacts(){  
            List<Contact> contacts = service.getContacts();  
            //OO的方法将对象的数据转成json字符串;  
            try {  
                JSONArray jsonArray = new JSONArray();//新建json数组  
                for(Contact contact : contacts){  
                    JSONObject item = new JSONObject();//新建json对象  
                    item.put("id", contact.getId());  
                    item.put("name", contact.getName());  
                    item.put("mobile", contact.getMobile());  
                    jsonArray.put(item);  
                }  
                String json = jsonArray.toString();//得到json字符串;  
//              webView.loadUrl("javascript:show('[{""},{}]')");  
                webView.loadUrl("javascript:show('"+ json +"')"); //调用js的show方法;  
                  
            } catch (JSONException e) {  
                e.printStackTrace();  
            }  
        }  
          
        public void call(String mobile){  
            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + mobile));  
            startActivity(intent);  
        }  
    }  
}  
3. AndroidManifest.xml 
<?xml version="1.0" encoding="utf-8"?>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    package="cn.itcast.html"  
    android:versionCode="1"  
    android:versionName="1.0" >  
  
    <uses-sdk android:minSdkVersion="8" />  
  
    <application  
        android:icon="@drawable/ic_launcher"  
        android:label="@string/app_name" >  
        <uses-library android:name="android.test.runner"/>  
        <activity  
            android:name=".MainActivity"  
            android:label="@string/app_name" >  
            <intent-filter>  
                <action android:name="android.intent.action.MAIN" />  
  
                <category android:name="android.intent.category.LAUNCHER" />  
            </intent-filter>  
        </activity>  
    </application>  
      
    <uses-permission android:name="android.permission.CALL_PHONE"/>  
    <uses-permission android:name="android.permission.INTERNET"/>  
      
    <instrumentation android:name="android.test.InstrumentationTestRunner"   
        android:targetPackage="cn.itcast.html" android:label="My File Test" />  
  
</manifest>  
4./layout/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" >  
  
    <WebView  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:id="@+id/webView"  
        />  
  
</LinearLayout>  



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值