1

I'm developing a bridge and am able to send a message from JS to Java. But now, I want to send a message back to JS.

I know about a way of doing it using Activity.startActivityForResult(intent, request_code) And then calling webView.evaluateJavascript but I don't want the intent part.

Is there a way to create an empty intent (new Intent() doesn't work) or an alternative to this method?

Filip
  • 855
  • 2
  • 18
  • 38
  • 1
    You might consider editing your question to explain exactly what you mean by "a bridge", and to show a [mcve] of how you are sending messages from JavaScript to Java. For example, if this is a `WebView` and you are using message channels for JS -> Java, you can use the same thing for Java -> JS: https://commonsware.com/blog/2017/01/23/replacing-addjavascriptinterface-html-message-channels.html – CommonsWare May 16 '21 at 20:45

2 Answers2

0

You can register an interface in Java with JavaScript through which JavaScript can call your native Java functions. Here you can find more details:

https://developer.android.com/guide/webapps/webview#BindingJavaScript

Khawar Raza
  • 15,870
  • 24
  • 70
  • 127
  • Yes. as per my question, I've already done that. Now I want the opposite. That is, send messages from Java to JavaScript. – Filip May 16 '21 at 21:06
  • Sorry I misread your question. Have you tried calling JS functions like this:. webView.loadUrl("javascript:foo('test message')"); – Khawar Raza May 16 '21 at 21:11
  • Here foo is the JavaScript function name and we are passing string as parameter. – Khawar Raza May 16 '21 at 21:13
0

I made a working Android Studio Example send data from Android java to WebView js and from WebView to Android

https://stackoverflow.com/a/63539217/1023540

(it was downloaded 191 times :-)

And
  • 73
  • 2
  • 7