gpt4 book ai didi

javascript - 是否可以在没有 webview 的情况下从 android 调用方法 javascript?

转载 作者:行者123 更新时间:2023-11-29 22:04:07 25 4
gpt4 key购买 nike

在我的 android 应用程序中需要调用 javascript 函数。如果我使用 webview 从 android 调用工作正常。但我想在没有 webview 的情况下调用,我使用 android 作为客户端,使用 node.js 作为服务器,我想在没有 webview 的情况下进行通信。

例如在我的 javascript 中我有这个函数

function test(){
//script for insert data here
}

我想在没有 webview 的情况下从 android 调用测试函数,如何实现?我该怎么办?

谢谢。

最佳答案

2018 年更新:AndroidJSCore 已被 LiquidCore 取代,它基于 V8.它不仅包括 V8 引擎,而且还提供所有 Node.js。

原始答案:参见 AndroidJSCore .它是适用于 Android 的 JavaScriptCore 的完整端口,运行 JavaScript 时没有笨重、缓慢的 WebView。

示例 fragment :

JSContext context = new JSContext();
context.evaluateScript("a = 10");
JSValue newAValue = context.property("a");
System.out.println(df.format(newAValue.toNumber())); // 10.0
String script =
"function factorial(x) { var f = 1; for(; x > 1; x--) f *= x; return f; }\n" +
"var fact_a = factorial(a);\n";
context.evaluateScript(script);
JSValue fact_a = context.property("fact_a");
System.out.println(df.format(fact_a.toNumber())); // 3628800.0

文档:http://ericwlange.github.io/

关于javascript - 是否可以在没有 webview 的情况下从 android 调用方法 javascript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21928450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com