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 acplishing this?
Thanks for your insights!
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 acplishing this?
Thanks for your insights!
Share Improve this question asked Sep 15, 2011 at 23:26 cottonBallPawscottonBallPaws 21.6k37 gold badges128 silver badges176 bronze badges 2- What you're doing doesn't sound like you're porting an app. – Falmarri Commented Sep 15, 2011 at 23:42
- @Falmarri, thanks for the really helpful ment ;) Yes I am porting this app to Android. However, there is a lot of well written and well tested backend code that has no real incentive to be rewritten in Java. This has nothing to do with the front end experience. Thanks. – cottonBallPaws Commented Sep 15, 2011 at 23:56
1 Answer
Reset to default 11From 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.