简体   繁体   English

如何从javascript调用java方法?

[英]How to call java method from javascript?

I have one html file with applet class and one java file in same directory.我在同一目录中有一个 html 文件和一个小程序 class 和一个 java 文件。 While I am calling java method from javascript using applet I got an error like:当我使用小程序从 javascript 调用 java 方法时,出现如下错误:

applet method is not a function

How to call java method from javascript?如何从javascript调用java方法?

If your applet name is "myApp". 如果您的小程序名称是“ myApp”。

Then you have this method inside it for example, 然后,您可以在其中包含此方法,

public void hi() {
Graphics g = getGraphics();
g.drawString("wads up", 10, 10);}

You can call 你可以打电话

<INPUT type="button" value="call method"    
onClick = "document.appName.hi()">

Update 更新资料

if you wish to accept parameters in your app, you need to specify this in your applet codes: 如果您希望在应用程序中接受参数,则需要在applet代码中指定此参数:

String para = this.getParameter("fromPage");

and you can have 你可以有

<PARAM name="fromPage" value="Param Sent to Applet!">

from within the applet tags to pass it to the applet 从applet标签内传递到applet

I have a technique to call javascript from java or java from javascript. Do this on web:我有一种技术可以从 java 调用 javascript 或从 javascript 调用 java。在 web 上执行此操作:

<a href="file:///android_asset/YourFile.html?q=true"></a>

And do this on java:在 java 上执行此操作:

webview1.loadUrl("file:///android_asset/YourFile.html");
new Timer().scheduleAtFixedRate(new TimerTask() {
    @Override
    public void run() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (webview1.getUrl().contains("?q=true")) {
                    webview1.loadUrl("file:///android_asset/YourFile.html");
                    
// YOUR CODE HERE
                }
            }
        });
    }
}, 20, 20);

How it works?怎么运行的? Web can change the URL easily. Web 可以轻松更改 URL。 (i use "?q=true") and java can get the webview's URL. every 20ms, java get the url and check if the url contains "?q=true" then run the code. (我使用“?q = true”)和java可以获得webview的URL。每20ms,java获得url并检查url是否包含“?q = true”然后运行代码。 you can also call a javascript function using java by reversing the code!您也可以通过反转代码使用 java 调用 javascript function!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM