Android在没有webview的情况下使用参数调用javascript函数
Android call javascript function with parameters without webview
我想在android应用程序中调用一个带有Java参数的javascript函数,我不需要在webview中加载它,因为我只需要调用它并从assets文件夹中的JS文件中获取结果。
我在iOS上使用JavascriptCore完成了这项工作,但在android中找不到相同的功能。
查阅了AndroidJSCore和Rihno,但文档和教程不清楚这个主题。
我将JS文件加载到一个String中,此外,我无法了解如何发送参数和获取结果。
以下是如何将文件加载到字符串中:
AssetManager assetManager = getAssets();
String jsFile;
// To load js file
InputStream input;
try {
input = assetManager.open("authenticate.js");
int size = input.available();
byte[] buffer = new byte[size];
input.read(buffer);
input.close();
// byte buffer into a string
jsFile = new String(buffer);
resultTV.setText(jsFile);
Log.d("TAG", jsFile);
} catch (IOException e) {
e.printStackTrace();
}
要发送的参数来自Edittext。
javascript函数接受2个参数并返回JSON
function authenticate(uName, pWord)
{
var authenString = JSON.stringify(authenJSON);
return authenString;
}
感谢您的帮助。
以下是我如何在Android中使用Rhino:
/**
*
* @param javaScriptCode
* @param functionNameInJavaScriptCode
* @param params Do not pass an array of primitives! For example if passing doubles, pass Double[], not double[]
* @return
*/
public Map<String,Object> execute(String javaScriptCode, String functionNameInJavaScriptCode, Iterable<String> returnObjectKeys, Object... params){
Map<String,Object> rtn = null;
// Every Rhino VM begins with the enter()
// This Context is not Android's Context
Context rhino = Context.enter();
// Turn off optimization to make Rhino Android compatible
rhino.setOptimizationLevel(-1);
try {
final Object[] parameters = new Object[params.length + 1];
for(int i=0; i < params.length; i++){
parameters[i] = params[i];
}
parameters[parameters.length - 1] = BuildConfig.DEBUG;
Scriptable scope = rhino.initStandardObjects();
rhino.evaluateString(scope, javaScriptCode, "JavaScript", 1, null);
// Get the functionName defined in JavaScriptCode
Object obj = scope.get(functionNameInJavaScriptCode, scope);
if (obj instanceof Function) {
Function jsFunction = (Function) obj;
// Call the function with params
Object jsResult = jsFunction.call(rhino, scope, scope, parameters);
if(jsResult == null){
return null;
}
Scriptable s = (Scriptable) jsResult;
rtn = convert(s, returnObjectKeys);
}
else {
throw new IllegalArgumentException("No function " + functionNameInJavaScriptCode + " found in supplied script");
}
} finally {
Context.exit();
}
return rtn;
}
private Map<String,Object> convert(Scriptable object, Iterable<String> keys){
Map<String,Object> rtn = new HashMap<>();
for(String s : keys){
if(object.has(s,object)){
rtn.put(s, object.get(s, object));
}
}
return rtn;
}
我想我从SO那里得到了大部分,但现在找不到问题了。
相关文章:
- 使用html表单中的参数调用JavaScript函数
- 如何在ES6类中使用参数调用函数
- 如何使用OnClick事件通过参数调用来调用PHP函数
- Worklight Client使用自定义BODY参数调用过程
- 如何在jQuery AJAX中使用参数调用PHP函数
- 如何使用参数调用java脚本函数,其中参数来自字符串变量
- 使用数组-apply()中的参数调用函数,但不使用上下文参数
- 如何使用JQuery.ech()中分配给变量的参数调用函数
- 如何用参数调用coffescript函数
- 使用与父函数相同的参数调用函数
- 使用动态参数调用函数
- 使用来自 asp.net 的参数调用 Java 脚本函数
- 设置 JavaScript 以使用不同的参数调用函数
- 在 javascript 函数中使用参数调用 java 函数
- 使用参数调用 JavaScript 处理程序
- 如何使用在 jscript 中声明的参数调用 Html.RenderAction()
- Firebase 承诺 - Query.once 失败:使用 1 个参数调用.预计至少 2
- 有没有办法通过另一个函数参数调用函数
- 在 PHP 中使用多个参数调用 js 函数
- 使用来自另一个函数的对象参数调用函数