Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Object[] params = new Object[] { "javaScriptParam" };
- // Every Rhino VM begins with the enter()
- // This Context is not Android's Context
- Context rhino = Context.enter();
- // Turn off optimization to make Rhino Android compatible
- rhino.setOptimizationLevel(-1);
- try {
- Scriptable scope = rhino.initStandardObjects();
- // Note the forth argument is 1, which means the JavaScript source has
- // been compressed to only one line using something like YUI
- rhino.evaluateString(scope, "function hello(){ return 'Hello world!'; }", "JavaScript", 1, null);
- // Get the functionName defined in JavaScriptCode
- Object obj = scope.get("hello", scope);
- if (obj instanceof Function) {
- Function jsFunction = (Function) obj;
- // Call the function with params
- Object jsResult = jsFunction.call(rhino, scope, scope, params);
- // Parse the jsResult object to a String
- String result = Context.toString(jsResult);
- Toast.makeText(MainActivity.this, "Ket qua: " + result, Toast.LENGTH_SHORT).show();
- }
- } finally {
- Context.exit();
- }
- http://stackoverflow.com/questions/7544671/how-to-call-javascript-from-android
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement