Objective
Dynamic execution of scripts can effectively reduce the problems caused by the hard coding of important functions, especially depending on the application of third parties, can be upgraded by dynamic script + online parameters (such as ally online parameters) without updating the application.
First, prepare
Project Address: Https://developer.mozilla.org/en-US/docs/Rhino
Project Description: Developed by Mozilla, JavaScript is a java-based implementation.
Project use: Copy Js.jar to Project engineering.
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/
Second, the Code
Mainly implemented from Java in the implementation of JS functions, from the JS call Java method, code:
public class Mainactivity extends activity {@Override protected void onCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main); TextView Text1 = (TextView) Findviewbyid (Android.
R.ID.TEXT1); TextView Text2 = (TextView) Findviewbyid (Android.
R.ID.TEXT2);
Text1.settext (RunScript (java_call_js_function, "Test", new string[] {}));
Text2.settext (RunScript (js_call_java_function, "Test", new string[] {})); /** Java Execute JS method/private static final String java_call_js_function = "FUNCTION Test () {return ' farmer Uncle JAVA Call JS Rhino ';
}"; /** JS calls the method in JAVA * * private static final String js_call_java_function =//"var Scriptapi = java.lang.Class.forNam E (\ "" + MainActivity.class.getName () + "\", True, Javaloader); "+//" var methodread = Scriptapi.getmethod (\ "Jscal Ljava\ ", [java.lang.String]);" +//"function Jscalljava (URL{return methodread.invoke (null, URL);} "
+//"function Test () {return Jscalljava ();}"; /** * Execute JS * * @param js JS code * @param functionname JS method name * @param functionparams js Method Parameters * @return */public string RunScript (String js, string functionname, object[] functionparams) {context RH
Ino = Context.enter ();
Rhino.setoptimizationlevel (-1);
try {scriptable scope = rhino.initstandardobjects ();
Scriptableobject.putproperty (Scope, "Javacontext", Context.javatojs (Mainactivity.this, scope));
Scriptableobject.putproperty (Scope, "Javaloader", Context.javatojs (MainActivity.class.getClassLoader (), scope));
Rhino.evaluatestring (Scope, JS, "mainactivity", 1, NULL);
function function = (function) scope.get (functionname, scope);
Object result = Function.call (rhino, scope, scope, functionparams); if (Result instanceofString) {return (string) result; else if (result instanceof Nativejavaobject) {return (String) (nativejavaobject) result). getdefaultval
UE (STRING.CLASS); else if (result instanceof NativeObject) {return (String) (nativeobject) result). Getdefaultvalue (Strin
G.class);
return result.tostring ()//(String) Function.call (Rhino, scope, scope, functionparams);
finally {context.exit ();
} public static string Jscalljava (String URL) {return "Farmer Uncle JS call Java Rhino"; }
}
Third, download
Samplerhino.zip (2013-12-05)
V. Other
Note that Js.jar may not be confusing when confused, please refer to Article 4.1 's method.
Author: cnblogs Peasant Uncle