不多说,先上代码,这是我们加载的页面代码:
<html>
<head>
<title>JS calls Android Method</title>
<script language="javascript">
function addButton() {
var html = "<input type=\"button\" value=\"New Button\" οnclick=\"newButton();\">";
document.getElementById("add").innerHTML = html;
}
function newButton() {
alert("I am new button");
}
</script>
</head>
<body>
<h1>JS on Android</h1>
<script type="text/javascript">
document.write(android.gps("<i>", "</i>"));
</script>
</br>
<h3>
<script type="text/javascript">
document.write(package.isPackageInstall("com.dolphin.browser.cn"));
</script>
</h3>
</br>
<p>New button will show below:</p>
<div id="add"></div>
</body>
</html>
1. 接下来是Javascript如何调用Android系统功能:
webview = new WebView(this);
webview.getSettings().setJavaScriptEnabled(true);
webview.addJavascriptInterface(new LocationJsObj(this), "android");
webview.addJavascriptInterface(new PackageJsObj(this), "package");
webview.loadUrl("file:///android_asset/Location.html");
class LocationJsObj {
private final Context con;
public LocationJsObj(Context con) {
this.con = con;
}
public String gps(String top, String end) {
return top + "緯度:" + 20.5555 + ", 経度: " + 36.4522 + end;
}
}
class PackageJsObj{
private final Context mContext;
public PackageJsObj(Context context){
mContext = context;
}
public String isPackageInstall(String pacakgeName){
boolean isInstalled = false;
PackageManager pm = mContext.getPackageManager();
try {
PackageInfo packageInfo = pm.getPackageInfo(pacakgeName, 0);
if(null != packageInfo){
isInstalled = true;
}
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return pacakgeName + " is install:" + isInstalled;
}
}
2. Android代码调用Javascript的功能函数:
webview = new WebView(this);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("file:///android_asset/Location.html");
Button button = new Button(this);
button.setText("Add new button on web");
button.setOnClickListener(mButtOnClickListener);
private final OnClickListener mButtOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
webview.loadUrl("javascript:addButton()");
}
};
合并后,完整代码:
package com.example.javascripttest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.LinearLayout;
public class JavaScriptTest extends Activity {
private WebView webview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(generateConentView());
}
private LinearLayout generateConentView(){
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT));
linearLayout.setOrientation(LinearLayout.VERTICAL);
webview = new WebView(this);
webview.getSettings().setJavaScriptEnabled(true);
webview.addJavascriptInterface(new LocationJsObj(this), "android");
webview.addJavascriptInterface(new PackageJsObj(this), "package");
webview.loadUrl("file:///android_asset/Location.html");
LinearLayout.LayoutParams wvLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
wvLayoutParams.weight = 1.0F;
webview.setLayoutParams(wvLayoutParams);
linearLayout.addView(webview);
Button button = new Button(this);
button.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
button.setText("Add new button on web");
button.setOnClickListener(mButtOnClickListener);
linearLayout.addView(button);
return linearLayout;
}
private final OnClickListener mButtOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
webview.loadUrl("javascript:addButton()");
}
};
class LocationJsObj {
private final Context con;
public LocationJsObj(Context con) {
this.con = con;
}
public String gps(String top, String end) {
return top + "緯度:" + 20.5555 + ", 経度: " + 36.4522 + end;
}
}
class PackageJsObj{
private final Context mContext;
public PackageJsObj(Context context){
mContext = context;
}
public String isPackageInstall(String pacakgeName){
boolean isInstalled = false;
PackageManager pm = mContext.getPackageManager();
try {
PackageInfo packageInfo = pm.getPackageInfo(pacakgeName, 0);
if(null != packageInfo){
isInstalled = true;
}
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return pacakgeName + " is install:" + isInstalled;
}
}
}