Android APIとJavascriptの間で呼び合う
5656 ワード
多くは言わないで、先にコードをつけて、これは私達がロードしたページコードです.
<html>
<head>
<title>JS calls Android Method</title>
<script language="javascript">
function addButton() {
var html = "<input type=\"button\" value=\"New Button\" onclick=\"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;
}
}
}