javascript - Android webview: how to obtain result of evaluateJavascript in a method that invokes evaluateJavascript?
问题描述
As we know in Android, if we want to obtain the result returned by JavaScript code invocation, we can use webview.evaluateJavascript()
. For example:
webview.evaluateJavascript("1+2", new ValueCallback<String>() {
public void onReceiveValue(String value) {
//value should be 3
}
});
However, how can I further obtain the result in a method that invokes evaluateJavascript
? For example:
public void myFunc() {
String s;
webview.evaluateJavascript("1+2", new ValueCallback<String>() {
public void onReceiveValue(String value) {
s = value; //This line is only for illustration, it is not syntactically correct
}
});
//Use s to do other things
}
Of course the above snippet doesn't work, but is it possible to achieve similar functionality?
Thanks!
解决方案
我这样做:
String js = "window.openDrawer();";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
mWebView.evaluateJavascript(js, new ValueCallback<String>() {
@Override
public void onReceiveValue(String s) {
Log.i("MyApp", s);
}
});
}
我不知道写 '1+2' 是否足够 javascript 来获取返回值。您可能需要在 javascript 中声明该函数,然后让该函数返回 1+2 的结果。
就像是:
String js = "func SumIt(int a, int b) { return (a + b); }; SumIt(1, 2);"
推荐阅读
- spring - 使用 Vault 保护 Spring Cloud 配置凭据
- php - 有人知道 Humhub 如何加密密码吗?
- sharepoint-2013 - 如何使用共享点列表创建测验?仅 OOTB 版本 2013
- c - 如何对包含指向自身的指针的结构进行类型定义?
- typo3-7.6.x - TYPO3 前端登录在更新后停止工作
- vbscript - VBScript 脉冲 (0 & 1)
- php - 在 yii2 框架中回显 p 标签以及 isset 函数条件
- vba - VBA 在活动列中查找值
- javascript - 如何在不使用 XMLHttpRequest() 重新加载页面的情况下将 datepicker 值传递给 php;
- elasticsearch - Elasticsearch 聚合按 _key 排序