我可以在没有WebView的情况下运行Javascript,还是可以在没有Activity上下文的情况下运行WebView,例如在服务中? [英] Can I run Javascript without a WebView, or can a WebView run without an Activity context, such as in a Service?

查看:54
本文介绍了我可以在没有WebView的情况下运行Javascript,还是可以在没有Activity上下文的情况下运行WebView,例如在服务中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTML5(JavaScript)支持的应用程序,我想移植到Android。

I have a HTML5 (JavaScript) backed app that I would like to port to Android.

在某些情况下,JavaScript代码需要在后台服务中运行(没有UI,所以没有WebView)。例如,为了同步。

In some cases, the JavaScript code needs to run in a background Service (no UI, so no WebView). For example, in order to sync.

从研究过程来看,WebViews似乎没有活动上下文。除此之外,仅使用UI视图来运行代码似乎很愚蠢,但我不确定其他选择。

From researching this, it doesn't appear that WebViews run without an Activity Context. Beyond that, it seems silly to use a UI View only to run code in the first place, but I'm not sure of the alternatives.

完成此任务的最佳方法是什么?

What is the best approach for accomplishing this?

感谢您的见解!

推荐答案


从研究这一点来看,似乎没有活动上下文的WebViews运行

From researching this, it doesn't appear that WebViews run without an Activity Context

实际上,他们可以。如果您调用新的WebView(ctxt),其中 ctxt 是任何类型的上下文,它有效。 Leastways,几个月前我最后一次试用它。

Actually, they can. If you call new WebView(ctxt), where ctxt is any sort of Context, it works. Leastways, it worked the last time I tried it, a couple of months ago.


除此之外,使用UI视图似乎很愚蠢只是为了首先运行代码,但我不确定替代方案。

Beyond that, it seems silly to use a UI View only to run code in the first place, but I'm not sure of the alternatives.

寻找Rhino,V8的版本, spidermonkey,或已移植到Android的其他JavaScript引擎。特别是Rhino是SL4A的一部分,你可以从该项目获得一个Rhino JAR嵌入你的应用程序。

Look for versions of Rhino, V8, spidermonkey, or other JavaScript engines that have been ported to Android. Rhino, in particular, is part of SL4A, and you can get a Rhino JAR from that project to embed in your application.

这篇关于我可以在没有WebView的情况下运行Javascript,还是可以在没有Activity上下文的情况下运行WebView,例如在服务中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆