Last active
November 26, 2015 14:56
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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