package me.wjh.android.htmlui;
import java.util.List;
import me.wjh.android.htmlui.domain.Contact;
import me.wjh.android.htmlui.service.ContactService;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
public class MainActivity extends Activity {
private final static String TAG = "HtmlUIMainActivity";
private WebView webView;
private ContactService contactService;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
contactService = new ContactService();
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new ContactsPlugin(), "contactsAction");
webView.loadUrl("file:///android_asset/index.html");
}
/**
* 这个类提供出了一个视图和业务层通信的接口。HTML 中,通过这个类的实例,间接与业务 Bean 通信。
* 为什么不直接将业务类提供给 webView, 让 HTML 中直接访问到这个类。而多出这样 "插件"~~
* 我想:目前的这样一种架构,Activity 甚至有些类似于控制器的概念了。有点像 struts 中的 Action。
* 在使用了 struts 框架的项目架构中,Action 也是被划分到视图层的。它和JSP页面共同完成准备数据和页面跳转的工作。
* 因此,这里我们也不应该让 HTML 中的 JS 直接与业务层耦合。实现表现层和业务层的解耦
*/
private class ContactsPlugin {
/**
* 此方法将执行 JS 代码,调用 JS 函数:show()
* 实现,将联系人信息展示到 HTML 页面上
*/
@SuppressWarnings("unused")
public void getContacts() {
List<Contact> contacts =contactService.getContacts();
try {
JSONArray array = new JSONArray();
for(Contact contact : contacts) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", contact.getId());
jsonObject.put("mobile", contact.getMobile());
jsonObject.put("name", contact.getName());
array.put(jsonObject);
}
String json = array.toString();
webView.loadUrl("javascript:show('"+ json +"')");
} catch (JSONException e) {
Log.i(TAG, e.toString());
}
}
/**
* 拨号
*/
@SuppressWarnings("unused")
public void call(String phoneCode) {
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneCode));
startActivity(intent);
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
HTMLUI.rar (27个子文件)
HTMLUI
.project 842B
src
me
wjh
android
htmlui
service
ContactService.java 594B
domain
Contact.java 645B
MainActivity.java 2KB
AndroidManifest.xml 752B
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
drawable-mdpi
icon.png 3KB
layout
main.xml 382B
values
strings.xml 182B
assets
index.html 2KB
gen
me
wjh
android
htmlui
R.java 739B
default.properties 364B
.classpath 280B
bin
me
wjh
android
htmlui
R$layout.class 403B
service
ContactService.class 1KB
R$drawable.class 409B
R$attr.class 349B
domain
Contact.class 1KB
MainActivity$ContactsPlugin.class 3KB
R$id.class 394B
R.class 552B
R$string.class 436B
MainActivity.class 2KB
resources.ap_ 11KB
HTMLUI.apk 16KB
classes.dex 6KB
共 27 条
- 1
wenlin56
- 粉丝: 232
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页