简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM