简体   繁体   English

如何获取Android调用的JavaScript函数的返回值?

[英]How can I get the return value of a JavaScript function called by Android?

Is it possible in Android to call a JavaScript function (which is in my WebView) and get its return value in Java? Android是否可以调用JavaScript函数(位于WebView中)并在Java中获取其返回值?

I know I can use a JavascriptInterface (in Android), for this I need to call the interface from the js function, but I can't modify the JavaScript function... 我知道我可以使用JavascriptInterface(在Android中),为此,我需要从js函数调用该接口,但是我无法修改JavaScript函数...

So I would like something like below, is it possible? 所以我想要下面这样的东西,有可能吗?

JavaScript: JavaScript:

function hello(){
    return "world";
}

Android: Android:

String res = myWebView.loadUrl("javascript:hello()"); 
// res = "world"

Thank you 谢谢

Yes, it is possible. 对的,这是可能的。 I already do many javascript injection to people website before. 我之前已经在人们网站上做了很多JavaScript注入。 You just need to inject your own javascript in any website. 您只需要在任何网站中注入自己的JavaScript。

For example 例如

//simple javascript to pass value from javascript to native app
String javascript =  "javascript:function testHello(){return Android.hello('HAI'); testHello();}"

webview.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                //put your function in string and load the javascript when the page is finished load
                view.loadUrl(javascript);
            }
});

// 'Android' is your variable key for triggering the function ex: Android.hello(), Android.calculate()
// you can change to other name like 'APP', so in javascript be like this ex: APP.hello(), APP.calculate()

webview.addJavascriptInterface(new WebAppInterface(this), "Android");

//load website
webview.loadUrl(PageUrl);

In WebAppInterface is where you create function to detect the javascript you inject earlier 在WebAppInterface中,您可以在其中创建函数来检测之前注入的javascript

public class WebAppInterface {
    Activity mContext;

    public WebAppInterface(Activity c) {
        mContext = c;
    }

    //this function will get your value from the javascript earlier.
    //Android.hello('value')

    @JavascriptInterface
    public void hello(String value){
        //you will get HAI message in here
        Log.i("TAG",value);
    }
}

暂无
暂无

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

相关问题 如何返回由 HTML 按钮单击调用的 JavaScript function 的值? - How can I return a value of a JavaScript function, which is called by a HTML button click? 如何将PHP数组返回到调用它的javascript函数 - How can I return a PHP array to the javascript function that called it 我如何调用javascript函数并从javascript函数获取返回值 - How I can call javascript function and get the return value from javascript function 如何从javascript / jQuery中调用另一个函数的函数获取返回值? - How can I get the return value from a function calling another function in javascript/jQuery? 如果参数是数字,我怎样才能让雪花中的 javascript 函数返回一个值,如果参数不是数字,我如何返回另一个值? - How can I get a javascript function in snowflake to return one value if the argument is numeric or another value if the argument is not numeric? 我们如何在Android应用程序中执行javascript函数并获取返回值? - How we can execute a javascript function and get a return value in our android application? 我可以在Spidermonkey中执行Javascript函数并获取返回值吗? - Can I execute a Javascript function inside Spidermonkey and get the return value? 如何在循环内运行异步 function 并获取返回值(Node.js/Javascript) - How can I run an asynchronous function inside a loop and get a return value (Node.js/Javascript) 如何从 JavaScript 中的 Handler 函数获取返回值? - How do I get a return value from a Handler function in JavaScript? 如何在HTML中打印JavaScript函数的返回值? - How can I print the return value of a JavaScript function in HTML?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM