Running JavaScript in a service on Android

I have business logic written in JavaScript, this code is used in conjunction with other applications other than android.

What is the best way to use the functions in this JavaScript snippet from a service on Android.

AFAIK, are there 2 options?

  • V8, built into the standard WebView and ultrafast, without additional apk overlays.
  • Rhino, what is difficult to do on Android?

Focusing on V8 / Webview, when I try to access WebView, with any function, I get:

All WebView methods must be called on the UI thread. Future versions of WebView may not support use on other threads. 

A warning is noted, it does not even work. When I install webviewclient, I get nothing after loading the url.

My question consists of 3 parts;

1) Has anyone been successful with running javascript in webview without a user interface thread?

2) How to get results from functions inside javascript, does the webview "addJavascriptInterface" interface support loading a parameter and sending it back to java?

3) If one of the above is impossible. I think I’ll go for Rhino, any advice will be appreciated, I only saw a few blogs complaining about problems with getting it on Android and I wonder if there is a β€œgo to” version for a supported android.

+8
javascript android android-service android-webview rhino
source share
1 answer

Could not find anything regarding V8 from depth of service.

The use of Rhino has ended, but the word of warning for everyone who follows in my footsteps is incredibly slow.

Just grab the jar from the official latest Rhino distribution from https://developer.mozilla.org/en-US/docs/Rhino/Download_Rhino?redirectlocale=en-US&redirectslug=RhinoDownload

js.jar is what you need in zip. js-14 is a larger version compatible with java 1.4 that you don't need.

The integration was simple, just pull the jar into your libs folder.

Below I scrap the webpage using javascript (turning the data into a more formatted json). Using the parse.js script, I made access to the resource folder.

Rhino does not ship with the DOM, and env.js crashes with stackoverflow errors. In general, I would say that this solution is slow and not supported ...

 public static void sync(Context context, ){ String url = BASE_URL; String html = Utils.inputStreamToString(Utils.getHTTPStream(url)); timeList.add(System.currentTimeMillis()); if(html == null){ Utils.logw("Could not get board list."); return; } String parsingCode = null; try { parsingCode = Utils.inputStreamToString(context.getAssets().open("parse.js")); } catch (IOException e) { Utils.logw("Could not get board parser js"); return; } // Create an execution environment. org.mozilla.javascript.Context cx = org.mozilla.javascript.Context.enter(); // Turn compilation off. cx.setOptimizationLevel(-1); try { // Initialize a variable scope with bindnings for // standard objects (Object, Function, etc.) Scriptable scope = cx.initStandardObjects(); ScriptableObject.putProperty( scope, "html", org.mozilla.javascript.Context.javaToJS(html, scope)); //load up the function cx.evaluateString(scope, parsingCode,"parseFunction", 1 , null); // Evaluate the script. Object result = cx.evaluateString(scope, "myFunction()", "doit:", 1, null); JSONArray jsonArray = new JSONArray(result.toString()); 
+6
source share

All Articles