T
T
Tolik2015-03-04 19:06:42
Android
Tolik, 2015-03-04 19:06:42

How to make a bridge between JavaScript inside a WebView and Java for an android application?

I want to try to make a program based on this video: https://www.youtube.com/watch?v=uVqp1zcMfbE
system commands (known as system() in C/C++). From the Java side, there should be no problem in using one or the other. A couple of imports, a couple of lines... But how to make a "bridge"? If possible

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Lebedev, 2015-03-04
@Diel

Look at this WebView method . It allows you to call an embedded Java object inside a page via Javascript.
If you're too lazy to follow the link:

class JsObject {
    @JavascriptInterface
    public String toString() { return "injectedObject"; }
 }
 webView.addJavascriptInterface(new JsObject(), "injectedObject");
 webView.loadData("", "text/html", null);
 webView.loadUrl("javascript:alert(injectedObject.toString())");

Be careful with this type of interaction with the app, this is a hack window.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question