public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String url = new String("file:///android_asset/Map.html");
setContentView(R.layout.leaflet_map);
this.setTitle("Location Map");
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
initComponent();
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setRenderPriority(RenderPriority.HIGH);
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
// multi-touch zoom
webSettings.setBuiltInZoomControls(true);
/* webSettings.setDisplayZoomControls(false);*/
myWebView.getSettings().setLoadsImagesAutomatically(true);
myWebView.setWebChromeClient(new WebChromeClient());
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("file:///android_asset/Map.html");
myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
myWebView.setWebViewClient(new WebViewClient());
mapTextView.setText("Hello Map!!!!");
}
public void initComponent(){
generateId();
getGeoLocation();
}
在這裏,我想打電話給我的JavaScript函數:如何從我的Android活動中調用Javascript函數?
public void getGeoLocation() {
// creating GPS Class object
gps = new com.getlocation.periscope.GPSTracker(this);
// check if GPS location can get
if (gps.canGetLocation()) {
Log.d("Your Location", "latitude:" + gps.getLatitude()
+ ", longitude: " + gps.getLongitude());
Double latitude = gps.getLatitude();
Double longitude = gps.getLongitude();
try{
myWebView.loadUrl("javascript:latLong('"+latitude+"','"+longitude+"')");
}catch(Exception ex){
Toast.makeText(this,"Exception type"+ex, Toast.LENGTH_LONG).show();
}
} else {
// Can't get user's current location
alert.showAlertDialog(this, "GPS Status",
"Couldn't get location information. Please enable GPS",
false);
// stop executing code by return
return;
}
Toast.makeText(
this,
"latitude:" + gps.getLatitude() + ", longitude: "
+ gps.getLongitude(),Toast.LENGTH_LONG).show();
}
請幫我解決這個問題。 – Ricky
請在您的問題中添加更多信息,但您不清楚您的問題。另外,請正確地將您的代碼格式化爲代碼塊。 –
@ Kent Hawkings先生,我想從我的活動類中調用我的JavaScript函數。 – Ricky