Advertisement
thieumao

Run JS

Oct 28th, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.08 KB | None | 0 0
  1. Object[] params = new Object[] { "javaScriptParam" };
  2. // Every Rhino VM begins with the enter()
  3. // This Context is not Android's Context
  4. Context rhino = Context.enter();
  5. // Turn off optimization to make Rhino Android compatible
  6. rhino.setOptimizationLevel(-1);
  7. try {
  8. Scriptable scope = rhino.initStandardObjects();
  9. // Note the forth argument is 1, which means the JavaScript source has
  10. // been compressed to only one line using something like YUI
  11. rhino.evaluateString(scope, "function hello(){ return 'Hello world!'; }", "JavaScript", 1, null);
  12. // Get the functionName defined in JavaScriptCode
  13. Object obj = scope.get("hello", scope);
  14. if (obj instanceof Function) {
  15. Function jsFunction = (Function) obj;
  16. // Call the function with params
  17. Object jsResult = jsFunction.call(rhino, scope, scope, params);
  18. // Parse the jsResult object to a String
  19. String result = Context.toString(jsResult);
  20. Toast.makeText(MainActivity.this, "Ket qua: " + result, Toast.LENGTH_SHORT).show();
  21. }
  22. } finally {
  23. Context.exit();
  24. }
  25.  
  26. http://stackoverflow.com/questions/7544671/how-to-call-javascript-from-android
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement