Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. / class ScriptAPI
  2.  
  3. // These lines set the log method in the JavaScript
  4. // Any Java classes or methods can be accessed with **Packages** prefix
  5. private static final String RHINO_LOG = "var log = Packages.io.vec.ScriptAPI.log;";
  6. public static void log(String msg) {
  7. android.util.Log.i("RHINO_LOG", msg);
  8. }
  9.  
  10. public void runScript() {
  11. // Get the JavaScript in previous section
  12. String source = getScriptFromServer();
  13. String functionName = "hello";
  14. Object[] functionParams = new Object[] { "Android" };
  15.  
  16. // Every Rhino VM begins with the enter()
  17. // This Context is not Android's Context
  18. Context rhino = Context.enter();
  19.  
  20. // Turn off optimization to make Rhino Android compatible
  21. rhino.setOptimizationLevel(-1);
  22. try {
  23. Scriptable scope = rhino.initStandardObjects();
  24.  
  25. // This line set the javaContext variable in JavaScript
  26. ScriptableObject.putProperty(scope, "javaContext", Context.javaToJS(androidContextObject, scope));
  27.  
  28. // Note the forth argument is 1, which means the JavaScript source has
  29. // been compressed to only one line using something like YUI
  30. rhino.evaluateString(scope, RHINO_LOG + source, "ScriptAPI", 1, null);
  31.  
  32. // We get the hello function defined in JavaScript
  33. Function function = (Function) scope.get(functionName, scope);
  34.  
  35. // Call the hello function with params
  36. NativeObject result = (NativeObject) function.call(rhino, scope, scope, functionParams));
  37. // After the hello function is invoked, you will see logcat output
  38.  
  39. // Finally we want to print the result of hello function
  40. String foo = (String) Context.jsToJava(result.get("foo", result), String.class);
  41. log(foo);
  42. } finally {
  43. // We must exit the Rhino VM
  44. Context.exit();
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement