Android使用Rhino让java执行javascript的方法实例

本文介绍了一种在Android应用中通过Rhino库实现Java调用JavaScript和JavaScript调用Java的方法,无需Webview,提供了示例代码和下载链接,适用于快速高效地进行JavaScript操作。
摘要由CSDN通过智能技术生成

在Android中需要调用js函数,以前我们需要加载webview然后来进行js交互,今天教大家一个方法,不需要webview也能做到,那就是Rhino。

官网下载地址:

https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino/Download_Rhino

此教程使用的是最新版的Rhino 1.7.7.1编译

下面是我写的一个简单的测试代码:

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, "hello", new String[] {}));
        text2.setText(runScript(JS_CALL_JAVA_FUNCTION, "hello", new String[] {}));
    }
    /** Java执行js的方法 */
    private static final String JAVA_CALL_JS_FUNCTION = "function hello(){ return 'lovecode.cn 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 hello(){ 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 "lovecode.cn js call Java Rhino";
    }
}

我已测试通过,大家拿去就可以使用了。

欢迎转载,请说明出处。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

泡在网上的蜘蛛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值