Do not use WebView to execute JavaScript scripts in Android (Rhino)

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.