<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>JS calls Android Method</title>
<script language="javascript">
function audioOpen(){
roid.open();
}
</script>
</head>
<body>
<h1>JS on Android</h1>
<script type="text/javascript">
document.write(roid.gps("<i>", "</i>"));
</script>
<input type="button" value="打開" onclick="audioOpen();">
</body>
</html>
package com.bibby.web;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.webkit.WebView;
public class WebkitTest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
WebView wv = new WebView(this);
wv.getSettings().setJavaScriptEnabled(true);
JsObj jo = new JsObj(this);
wv.addJavascriptInterface(jo, "roid");
setContentView(wv);
wv.loadUrl("js2android.html");
}
class JsObj {
private Context con;
public JsObj(Context con) {
this.con = con;
}
public String gps(String top, String end) {
LocationManager locman = (LocationManager) con.getSystemService(Context.LOCATION_SERVICE);
Location loc = locman.getCurrentLocation("gps");
int lat = (int) (loc.getLatitude() * 1000000);
int lon = (int) (loc.getLongitude() * 1000000);
return top + "LAT:" + lat + " , LON: " + lon + end;
}
public void open() {
Toast.makeText(getApplicationContext(), "OPEN", Toast.LENGTH_LONG).show();
}
}
}
參考資料:
http://blog.csdn.net/nanjingjiangbiao/article/details/7681856
http://developer.android.com/guide/webapps/webview.html
http://android.yaohuiji.com/archives/734
http://yutayu.blogspot.tw/2010/11/android-webview.html
留言列表