最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 11

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)

  1. 暂无评论