开发者

Can I run Javascript without a WebView, or can a WebView run without an Activity context, such as in a Service?

开发者 https://www.devze.com 2023-04-05 10:05 出处:网络
I have a HTML5 (JavaScript) backed app 开发者_开发技巧that I would like to port to Android. In some cases, the JavaScript code needs to run in a background Service (no UI, so no WebView). For example

I have a HTML5 (JavaScript) backed app 开发者_开发技巧that I would like to port to Android.

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

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?

Thanks for your insights!


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

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.

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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消