主要是通过addJavaScipteInterface(Object obj,String Interfacename) 方法将一个java对象绑定到一个javaScipte对象中,javaScripte对象名就是interfaceName
package com.king.android.net;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import com.king.android.R;
/**
* 描述:webview与js互用
* 作者:Andy.Liu
* 时间: 2012-7-26 上午08:24:06
**/
public class WebAndJsActivity extends Activity {
private WebView mWebView = null;
private PersonalData mPersonalData; //保存数据
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview_layout);
mPersonalData = new PersonalData();
mWebView = (WebView) findViewById(R.id.wv_view);
mWebView.getSettings().setJavaScriptEnabled(true);
//把本类的一个实例添加到javascript全局对象window中,这样就可以使用windowPersonData来调用它的方法
mWebView.addJavascriptInterface(this, "PersonalData");
//加载网页
mWebView.loadUrl("file://android_asset/PersonalData.html");
}
public PersonalData getPersonalData(){
return mPersonalData;
}
//js脚本中调用显示的资料
class PersonalData
{
String mID;
String mName;
String mAge;
String mBlog;
public PersonalData()
{
this.mID="123456789";
this.mName="Android";
this.mAge="100";
this.mBlog="http://xxx.javaeye.com";
}
public String getID()
{
return mID;
}
public String getName()
{
return mName;
}
public String getAge()
{
return mAge;
}
public String getBlog()
{
return mBlog;
}
}
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import com.king.android.R;
/**
* 描述:webview与js互用
* 作者:Andy.Liu
* 时间: 2012-7-26 上午08:24:06
**/
public class WebAndJsActivity extends Activity {
private WebView mWebView = null;
private PersonalData mPersonalData; //保存数据
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview_layout);
mPersonalData = new PersonalData();
mWebView = (WebView) findViewById(R.id.wv_view);
mWebView.getSettings().setJavaScriptEnabled(true);
//把本类的一个实例添加到javascript全局对象window中,这样就可以使用windowPersonData来调用它的方法
mWebView.addJavascriptInterface(this, "PersonalData");
//加载网页
mWebView.loadUrl("file://android_asset/PersonalData.html");
}
public PersonalData getPersonalData(){
return mPersonalData;
}
//js脚本中调用显示的资料
class PersonalData
{
String mID;
String mName;
String mAge;
String mBlog;
public PersonalData()
{
this.mID="123456789";
this.mName="Android";
this.mAge="100";
this.mBlog="http://xxx.javaeye.com";
}
public String getID()
{
return mID;
}
public String getName()
{
return mName;
}
public String getAge()
{
return mAge;
}
public String getBlog()
{
return mBlog;
}
}
}
js脚本:window.onload= function(){
var personaldata = window.PersonalData.getPersonalData();
if(personaldata)
{
var Personaldata = document.getElementById("Personaldata");
pnode = document.createElement("p");
tnode = document.createTextNode("ID:" + personaldata.getID());
pnode.appendChild(tnode);
Personaldata.appendChild(pnode);
pnode = document.createElement("p");
tnode = document.createTextNode("Name:" + personaldata.getName());
pnode.appendChild(tnode);
Personaldata.appendChild(pnode);
pnode = document.createElement("p");
tnode = document.createTextNode("Age:" + personaldata.getAge());
pnode.appendChild(tnode);
Personaldata.appendChild(pnode);
pnode = document.createElement("p");
tnode = document.createTextNode("Blog:" + personaldata.getBlog());
pnode.appendChild(tnode);
Personaldata.appendChild(pnode);
}
if(personaldata)
{
var Personaldata = document.getElementById("Personaldata");
pnode = document.createElement("p");
tnode = document.createTextNode("ID:" + personaldata.getID());
pnode.appendChild(tnode);
Personaldata.appendChild(pnode);
pnode = document.createElement("p");
tnode = document.createTextNode("Name:" + personaldata.getName());
pnode.appendChild(tnode);
Personaldata.appendChild(pnode);
pnode = document.createElement("p");
tnode = document.createTextNode("Age:" + personaldata.getAge());
pnode.appendChild(tnode);
Personaldata.appendChild(pnode);
pnode = document.createElement("p");
tnode = document.createTextNode("Blog:" + personaldata.getBlog());
pnode.appendChild(tnode);
Personaldata.appendChild(pnode);
}
}
当然java代码中也可以直接调用js方法,这样就可以互相调用取得数据。
webView.loadUrl("javascpite:方法名()");