【问题标题】:How do I write an html file that runs JavaScript when an android app opens it?android 应用程序打开时如何编写运行 JavaScript 的 html 文件?
【发布时间】:2019-04-19 21:40:25
【问题描述】:

如何编写一个 html 文件,以便在特定的 android 应用程序运行 html 文件时它会运行 JavaScript? android 应用程序中有一个名为 examplefunction()

的函数

我期望的 html 看起来与此类似,但我无法正确处理。

<html>
<body>
<script>
document.write(javascript:Android.examplefunction())
</script>"
</body>
</html>

android 应用程序运行

webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(this, "Android");
webView.loadUrl("www.example.com/test.html");

我正在读这样的html

public void onPageFinished(WebView webView, String str) {
        webView.evaluateJavascript("(function(){return window.document.body.outerHTML})();", new ValueCallback<String>() {
                public void onReceiveValue(String str) {
                   (code that uses the output of examplefunction())
        }
   });
}

【问题讨论】:

    标签: android android-webview webviewclient


    【解决方案1】:

    javascript与android之间的通信以及将值从javascript传递给Android可以参考here

    您可以在html中使用以下脚本

     <!DOCTYPE html>
        <html>
        <body>
        <script>
         function showAndroidToast()  {
             android.showToast(Date());
         }
         showAndroidToast();
        </script>
        </body>
        </html> 
    

    在您的 java 代码中放置一个 showToast 方法,并在您的 java 代码中引用名称为“android”的 javascript 接口。

    【讨论】:

    • 我尝试了您的建议和几个变体,但是当它打开并读取 html 时,它会回复 &lt;script&gt;function showAndroidToast(){\nAndroid.examplefunction()\n}\nshowAndroidToast(); &lt;/script&gt; 而不是 &lt;script&gt; (output of example function) &lt;/script&gt;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-09
    • 2013-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多