您的位置:首页 > 移动开发 > Android开发

android学习笔记---53_采用网页设计软件界面,以及使用android系统内置的浏览器,利用js调用java方法

2013-05-18 19:28 1221 查看
Java技术qq交流群:JavaDream:251572072
2013/5/16
53_采用网页设计软件界面
------------------------------
1.注意这里可以把网页放到本地,也可以把文件放到互联网上.
2.如果放到互联网上的话,那么每次当应用启动的时候会通过
互联网访问这个网页,也就是每次都需要加载这个网页
-----------------------------------------------------
3.下面是使用网页写的界面,做拨打电话的程序
-------------------------------------------
4.新建项目:
htmlUI
------------------
a./htmlUI/src/com/credream/domain/Contact.java
package com.credream.domain;

public class Contact {
// 1.联系人的id
private Integer id;
//2.联系人的名字
private String name;
//3.联系人的电话
private String phone;
//4.联系人的存款
private Integer amount;
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 getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Integer getAmount() {
return amount;
}
public void setAmount(Integer amount) {
this.amount = amount;
}
public Contact(Integer id, String name, String phone, Integer amount) {
this.id = id;
this.name = name;
this.phone = phone;
this.amount = amount;
}

}
---------------------------------------------------------------------
c./htmlUI/src/com/credream/htmlui/HtmlUIActivity.java
package com.credream.htmlui;

import java.util.List;

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

import com.credream.domain.Contact;
import com.credream.service.ContactService;

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

public class HtmlUIActivity extends Activity {
private WebView webView;
//7.实例化业务bean
private ContactService contactService;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//1.找到这个webView控件,
webView = (WebView) this.findViewById(R.id.webView);
//2.通过下面的代码,就可以加载/htmlUI/assets/index.html这个网页了.
webView.loadUrl("file:///android_asset/index.html");
//6.让浏览器支持javascript代码:
webView.getSettings().setJavaScriptEnabled(true);

//3.这里可以给浏览器,添加javascript接口,第一个参数是,一个js对象,其实就是一个java类,第二个参数是
//javascript:contact.showcontacts()"这里指定的名字:contact.
webView.addJavascriptInterface(new JSObject(), "contact");

contactService = new ContactService();
}
//4.这里,就是js这个对象.
private final class JSObject{
//javascript:contact.showcontacts()
//13.<a href='javascript:contact.call(\""+ jsonobjs[y].phone+ "\")'>"+ jsonobjs[y].phone+ "</a>";
public void call(String phone){
//14.这个方法是说,当用户点击电话号码的时候,会自动的触发,这个call方法:javascript:contact.call
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+ phone));
startActivity(intent);
}
// javascript:contact.
//这个方法就是网页中js指定的方法.showcontacts()
public void showcontacts(){
// [{name:"xxx",amount:600,phone:"13988888"},{name:"bb",amount:200,phone:"1398788"}]
try {
//8.取得联系人的数据
List<Contact> contacts = contactService.getContacts();
//9.组拼成json格式的数据。
JSONArray jsonArray = new JSONArray();
for(Contact c : contacts){
JSONObject jsonObject = new JSONObject();
//10.添加名称到json对象中
jsonObject.put("name", c.getName());
//11.添加存款到json对象中
jsonObject.put("amount", c.getAmount());
//12.添加电话到json对象中
jsonObject.put("phone", c.getPhone());

jsonArray.put(jsonObject);
}
//
String json = jsonArray.toString();
//5.webView.loadUrl用这个api,调用javascript代码.
webView.loadUrl("javascript:show('"+ json+ "')");
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
-------------------------------------------------------------
d./htmlUI/src/com/credream/service/ContactService.java
package com.credream.service;

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

import com.credream.domain.Contact;

public class ContactService {
/**
* 用于获取联系人
* @return
*/
public List<Contact> getContacts(){
//1.添加测试数据
List<Contact> contacts = new ArrayList<Contact>();
contacts.add(new Contact(12, "李德伟", "13766666666", 13003));
contacts.add(new Contact(23, "孔斌", "130066006", 122003));
contacts.add(new Contact(98, "孙乐磊", "186768768", 10988787));
contacts.add(new Contact(76, "总监", "1565622566", 1666));
return contacts;
}
}
-------------------------------------------------------
e./htmlUI/assets/index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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){//jsondata接收一个字符串 [{name:"xxx",amount:600,phone:"13988888"},{name:"bb",amount:200,phone:"1398788"}]
var jsonobjs = eval(jsondata);
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].name;
td2.innerHTML = jsonobjs[y].amount;
td3.innerHTML = "<a href='javascript:contact.call(\""+ jsonobjs[y].phone+ "\")'>"+ jsonobjs[y].phone+ "</a>";
}
}
</script>

</head>
<!-- js代码通过webView调用其插件中的java代码 -->
<body onload="javascript:contact.showcontacts()">
<!-- javascript:contact.showcontacts() 这个contact是自定义的对象,还没有 定义,将在下面讲解中定义..-->
<table border="0" width="100%" id="personTable" cellspacing="0">
<tr>
<td width="35%">姓名</td><td width="30%" align="center">存款</td><td align="center">电话</td>
</tr>
</table>
<a href="javascript:window.location.reload()">刷新</a>
</body>

</html>
------------------------------------------------
f./htmlUI/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.credream.htmlui"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".HtmlUIActivity" >
<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"/>

</manifest>
------------------------------------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐