Skip to content

Instantly share code, notes, and snippets.

@GigigoGreenLabs
Last active November 26, 2015 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GigigoGreenLabs/58ac9462a9e47f815838 to your computer and use it in GitHub Desktop.
Save GigigoGreenLabs/58ac9462a9e47f815838 to your computer and use it in GitHub Desktop.
Call java method in a html page executing the java method in a custom webview
<!DOCTYPE html>
<html>
<body onload="checkInterface()">
<p id="demo"></p>
<script>
function checkInterface() {
var places = Mobile.loadItems() ;
var jsonPlaces = JSON.parse(places);
Mobile.show(places);
}
</script>
</body>
</html>
public class LoadItems {
private final List<PresentationPlace> items;
public LoadItems(List<PresentationPlace> items) {
this.items = items;
}
@JavascriptInterface
public String loadItems() {
return new Gson().toJson(items);
}
@JavascriptInterface
public void show(String toast) {
return Toast.make(context, toast, Toast.LENGTH_LONG).show();
}
}
@Bind(R.id.webView) WebView webView;
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setAllowFileAccessFromFileURLs(true);
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
//To request the geolocation in the webview
webView.getSettings().setGeolocationDatabasePath(getContext().getFilesDir().getPath());
webView.setWebChromeClient(new WebChromeClient() {
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
}
});
//The class LoadItems is called 'Mobile' in the javascript code
webView.addJavascriptInterface(new LoadItems(places), "Mobile");
webView.loadUrl("file:///android_asset/esri/arcGisMap.html");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment