5

Is it possible to add a JavaScript interface to the Android Browser the same way one can be added to the WebView Component as illustrated in this demo. My particular use case only needs JavaScript -> android so that I can send it back to the previous activity.

5
  • Just curious, why don't you use the code of the open source project you've pointed to? Is it not in there what you're looking for? Not sure what you mean with JS Interface, can you explain what it does? Commented Sep 22, 2010 at 2:25
  • I need the tighter integration of the Browser over a WebView. I need to click on a button of a webpage and trigger the return to the prior Activity (that launched the browser) Commented Sep 22, 2010 at 2:42
  • 1
    I think that's not possible... don't you think it will be dangerous? I mean, allowing that kind of things would be a BIG security issue. Commented Sep 22, 2010 at 4:35
  • the best you can do is use a webview for showing the webpage and add your JS interface to that. Commented Sep 22, 2010 at 5:25
  • Cristian, I don't disagree with you. I want only to trigger a return to the prior Activity in the stack. Commented Sep 22, 2010 at 12:14

2 Answers 2

9

You can invoke methods and functions in your webview by using javascript url's, e.g.

webview.loadUrl("javascript:somemethod()");

You will, of course, need to enable javascript on your webview:

webview.getSettings().setJavaScriptEnabled(true);

This is from java to javascript. If you want to invoke java code / android API's from javascript, use addJavascriptInterface()

webview.addJavascriptInterface(new MyJSJavaBridge(), "api");

All of this is shown in the example url you posted as well.

Sign up to request clarification or add additional context in comments.

2 Comments

sure, on the WebView you've described the same steps as in the WebViewDemo that I linked to in the question, but what about the Browser?
@vo van der Wijk i have question about this code - webview.loadUrl("javascript:somemethod()"); because in my app it does not work=( so can you explain what is wrong with my code which is not working - webview.loadUrl("javascript:document.getElementById('pass_id').value ='" + CLIENT_PWD + "')");
3

You can do it using jsinterface. First you need to make the browser jsinterface enabled and then you may call to Android method from the HTML of your browser.

You may , get a fair example and idea here ...

http://android-puremvc-ormlite.blogspot.com/2011/07/using-java-script-in-android-webview.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.