美文网首页
java代码调用js

java代码调用js

作者: 霁逸lei | 来源:发表于2022-06-20 09:16 被阅读0次

    1.webview.loadUrl

    2.jsevaluator 内部实现也是webview.loadUrl
    GitHub - codebymikey/js-evaluator-for-android: A library for running JavaScript in Android apps.

    image.png

    3.JS in Android (Google V8)
    J2V8就是对V8 JavaScript 引擎 的 Java 封装,所以在Java中如果引入了J2V8,我们就可以在Java中运行JavaScript代码。
    implementation 'com.eclipsesource.j2v8:j2v8:5.0.103@aar'

    https://gitee.com/huangxiaoguo/J2V8.git
    test.js
    var hisV1 = 0
    function myFunction(p1, p2,callback) {
        callback(p1,p2)
        hisV1 = p1
        return p1 * p2;
    }
    
    V8 runtime = V8.createV8Runtime();
    runtime.executeScript(loadJs("real_library/test.js"));
    V8Function function = new V8Function(runtime, new JavaCallback() {
        @Override
        public Object invoke(V8Object v8Object, V8Array v8Array) {
          Log.e("RealLibrary",  v8Array.getInteger(0)+"" + v8Object);
          return null;
        }
    });
    V8Array push = new V8Array(runtime).push(10).push(20).push(function);
    runtime.executeVoidFunction("myFunction",push);
    function.close();
    push.close();
    runtime.close();
    

    相关文章

      网友评论

          本文标题:java代码调用js

          本文链接:https://www.haomeiwen.com/subject/qhafvrtx.html