Skip to content

Instantly share code, notes, and snippets.

@omegasoft7
Created December 19, 2014 15:33
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 omegasoft7/6a23abf4bcd18cae6dfb to your computer and use it in GitHub Desktop.
Save omegasoft7/6a23abf4bcd18cae6dfb to your computer and use it in GitHub Desktop.
Connect Java and JavaScript. Call a java method on Javascript codes that are running in webview
There is a hack:
Bind some Java object so that it can be called from Javascript with WebView:
addJavascriptInterface(javaObjectCallback, "JavaCallback")
Force execute javascript within an existing page by
WebView.loadUrl("javascript: var result = window.YourJSLibrary.callSomeFunction();
window.JavaCallback.returnResult(result)");
(in this case your java class JavaObjectCallback should have a method returnResult(..))
Note: this is a security risk - any JS code in this web page could access/call your binded Java object. Best to pass some one-time cookies to loadUrl() and pass them back your Java object to check that it's your code making the call.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment