J2V8学习(1),2024大厂Android知识点总结

本文介绍了如何在Android项目中使用J2V8库来执行JavaScript代码,包括Integer和String的返回、获取JavaScript对象、操作数组及调用函数。通过示例展示了J2V8在Android开发中的具体用法,同时也分享了作者的学习经历和一份2024年Android开发的全套学习资料。
摘要由CSDN通过智能技术生成

buildscript {

repositories {

mavenCentral()

google()

jcenter()

}

}

//加入国内镜像防止比较大google一直无法下载

allprojects {

repositories {

maven { url ‘https://maven.aliyun.com/repository/google’ }

maven{ url ‘https://maven.aliyun.com/repository/jcenter’}

mavenCentral()

google()

jcenter()

}

}

implementation ‘com.eclipsesource.j2v8:j2v8:6.0.0@aar’

  • 使用Integer(返回最后一行Integer结果)

/**

  • 使用Integer(返回最后一行Integer结果)

  • 两个字符串连接起来并且返回了结果字符串的长度

*/

private void printV8Integer() {

V8 runtime = V8.createV8Runtime();

int result = runtime.executeIntegerScript(“” +

“var hello = 'hello, ';\n” +

“var world = ‘world!’;\n” +

“hello.concat(world).length;\n”);

Log.e(TAG, "JS result = " + result);//JS result = 13

runtime.release(true);

}

在这里插入图片描述

  • 使用String(返回最后一行结果)

/**

  • 使用String(返回最后一行结果)

*/

private void printV8String() {

V8 runtime = V8.createV8Runtime();

String result = runtime.executeStringScript(“” +

“var hello = 'hello, ';\n” +

“var world = ‘world!’;\n” +

“hello.concat(world);\n”);

Log.e(TAG, "JS result = " + result);//S result = hello, world!

runtime.release(true);

}

在这里插入图片描述

  • 获取Javascript对象

/**

  • 获取Javascript对象

  • 使用J2V8可以从Java中获取javascript对象的句柄

  • V8Object还提供了一些其他有用的方法。

  • getKeys()将返回与Object关联的键。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值