Android 中的 JavaScript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4211782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript in Android
提问by Piyush
Is it possible to use JavaScript in Android?? if so, how? Please provide some examples.
是否可以在 Android 中使用 JavaScript?如果是这样,如何?请提供一些例子。
Thanks.
谢谢。
回答by Eric Lange
I am way late to the party here, but I had this exact need. iOS 7 now includes JavaScriptCore natively and it is really easy to use (despite limited documentation). The problem is that I didn't want to use it unless I could also use something similar on Android. So I created the AndroidJSCore project. It allows you to use your JavaScript code natively in Android without requiring a bulky WebView and injection. You can also seamlessly make asynchronous calls between Java and Javascript.
我来这里参加派对已经很晚了,但我确实有这个需要。iOS 7 现在原生包含 JavaScriptCore,它非常易于使用(尽管文档有限)。问题是我不想使用它,除非我也可以在 Android 上使用类似的东西。所以我创建了AndroidJSCore 项目。它允许您在 Android 中本地使用您的 JavaScript 代码,而无需庞大的 WebView 和注入。您还可以在 Java 和 Javascript 之间无缝地进行异步调用。
Update 27 Mar 17:AndroidJSCore has been deprecated in favor of LiquidCore. LiquidCore is based on V8 rather than JavascriptCore, but works essentially same. See the documentation on using LiquidCore as a raw Javascript engine.
2017年 3 月 27 日更新:AndroidJSCore 已被弃用,取而代之的是LiquidCore。LiquidCore 基于 V8 而不是 JavascriptCore,但工作原理基本相同。请参阅有关使用 LiquidCore 作为原始 Javascript 引擎的文档。
From the documentation:
从文档:
... to get started, you need to create a JavaScript JSContext. The execution of JS code
occurs within this context, and separate contexts are isolated virtual machines which
do not interact with each other.
...要开始,您需要创建一个 JavaScript JSContext。JS 代码的执行发生在这个上下文中,单独的上下文是相互不交互的隔离虚拟机。
JSContext context = new JSContext();
This context is itself a JavaScript object. And as such, you can get and set its properties. Since this is the global JavaScript object, these properties will be in the top-level context for all subsequent code in the environment.
这个上下文本身就是一个 JavaScript 对象。因此,您可以获取和设置其属性。由于这是全局 JavaScript 对象,因此这些属性将位于环境中所有后续代码的顶级上下文中。
context.property("a", 5);
JSValue aValue = context.property("a");
double a = aValue.toNumber();
DecimalFormat df = new DecimalFormat(".#");
System.out.println(df.format(a)); // 5.0
You can also run JavaScript code in the context:
您还可以在上下文中运行 JavaScript 代码:
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
You can also write functions in Java, but expose them to JavaScript:
您也可以用 Java 编写函数,但将它们暴露给 JavaScript:
JSFunction factorial = new JSFunction(context,"factorial") {
public Integer factorial(Integer x) {
int factorial = 1;
for (; x > 1; x--) {
factorial *= x;
}
return factorial;
}
};
This creates a JavaScript function that will call the Java method factorialwhen
called from JavaScript. It can then be passed to the JavaScript VM:
这将创建一个 JavaScript 函数,该函数将factorial在从 JavaScript调用时调用Java 方法。然后可以将其传递给 JavaScript VM:
context.property("factorial", factorial);
context.evaluateScript("var f = factorial(10);")
JSValue f = context.property("f");
System.out.println(df.format(f.toNumber())); // 3628800.0
回答by Joel
Do you mean something like making a native app using Javascript? I know there are tools like Titanium Mobilethat let you make native apps using web tools/languages.
您的意思是使用 Javascript 制作本机应用程序吗?我知道有像Titanium Mobile这样的工具可以让你使用网络工具/语言制作原生应用程序。
You could also make Web Apps. There are loads of resources and tutorials out there for that. Just search "Android Web App tutorial" or something similar.
你也可以制作网络应用程序。那里有大量资源和教程。只需搜索“Android Web App 教程”或类似内容。
回答by Alex Djioev
Yes you can, just create a wrap up code that points to html page and includes your javascript and css.
是的,您可以,只需创建一个指向 html 页面并包含您的 javascript 和 css 的包装代码。
There are different libraries that can help you with this:
有不同的库可以帮助您解决这个问题:

