使用Rhino讓java執行javascript的方法執行個體_java

來源:互聯網
上載者:User

下載Rhino https://developer.mozilla.org/en-US/docs/Rhino

把js.jar拷貝到項目工程

實現從Java中執行js中的函數、從js中調用Java中的方法,代碼:

複製代碼 代碼如下:

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執行js的方法 */
    private static final String JAVA_CALL_JS_FUNCTION = "function Test(){ return '農民伯伯 java call js Rhino'; }";

    /** js調用Java中的方法 */
    private static final String JS_CALL_JAVA_FUNCTION = //
    "var ScriptAPI = java.lang.Class.forName(\"" + MainActivity.class.getName() + "\", true, javaLoader);" + //
        "var methodRead = ScriptAPI.getMethod(\"jsCallJava\", [java.lang.String]);" + //
        "function jsCallJava(url) {return methodRead.invoke(null, url);}" + //
        "function Test(){ return jsCallJava(); }";

    /**
     * 執行JS
     *
     * @param js js代碼
     * @param functionName js方法名稱
     * @param functionParams js方法參數
     * @return
     */
    public String runScript(String js, String functionName, Object[] functionParams) {
        Context rhino = 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 instanceof String) {
                return (String) result;
            } else if (result instanceof NativeJavaObject) {
                return (String) ((NativeJavaObject) result).getDefaultValue(String.class);
            } else if (result instanceof NativeObject) {
                return (String) ((NativeObject) result).getDefaultValue(String.class);
            }
            return result.toString();//(String) function.call(rhino, scope, scope, functionParams);
        } finally {
            Context.exit();
        }
    }

    public static String jsCallJava(String url) {
        return "農民伯伯 js call Java Rhino";
    }
}

注意,混淆的時候js.jar可能混淆不過去,請參照文章4.1的方法。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.