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.
精彩评论