I want to invoke any JavaScript method from Android

Asked 1 years ago, Updated 1 years ago, 76 views

I use Android Studio to set up WebView in Java to display any HTML.
Therefore, I would like to interconnect Android with JavaScript in HTML.

First, for JavaScript→ Android (Java), I succeeded in calling any Java method on Android using @JavascriptInterface.Arguments and so on have crossed correctly.

On the other hand, I'm stuck because I don't know how to call any method in HTML from Android (Java).
There were some information that I could call in webview.loadUrl("javascript:hoge();"); but it doesn't show up even if I install an alert, so I think it's not called well.
webview.validateJavascript("hoge();", null); did the same.

I apologize for the inconvenience, but I would appreciate it if you could let me know.

javascript java android webview

2022-09-30 19:48

1 Answers

When you ask questions, be sure to write down the environmental information first as a minimum of manners.
For Android development, the OS version and vendor of the device, IDE (Android Studio) version, Android SDK version, targetSdkVersion value, etc.

In order to use alert() on the JS side, the WebView.setWebChromeClient() must have a WebChrome instance.

Note that JS can only be run after the page loading is complete.
Please refer to the following for details.


2022-09-30 19:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.