WebView JavaScript 桥文档
有没有关于 WebView JavaScript Bridge 的文档?我正在寻找描述“JavascriptInterface”中定义的方法的功能和支持的数据类型的文档。
例如,如果我定义以下内容:
public class JavaScriptInterface {
public int incrementNumber(int num) {
return num + 1;
}
如果我从 JavaScript 中调用此方法并在模拟器中运行它,则一切似乎都工作正常。如果我在 NexusOne 上运行此命令,则传入的“num”参数始终为“0”。
如果我将上面的内容更改为:
public class JavaScriptInterface {
public int incrementNumber(String num) {
// Leaving out try/catch
int tempNum = newRadius = Integer.parseInt(num);
return tempNum + 1;
}
...一切似乎都有效。所以我想知道 JavaScriptInterface 方法参数是否应该/只能是 String 类型?
相关资源: http://developer.android.com/reference/android/webkit/WebView.html http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface(java.lang.Object, java.lang.String) http://code.google.com/apis/maps/articles/android_v3.html
Is there any documentation regarding the WebView JavaScript Bridge? I am looking for documentation that describes the capabilities and supported data types for methods defined within the "JavascriptInterface".
For example if I define the following:
public class JavaScriptInterface {
public int incrementNumber(int num) {
return num + 1;
}
If I call this method from within JavaScript and run it in the emulator, everything seems to work fine. If I run this on my NexusOne, the passed in "num" argument is always "0".
If I change the above to:
public class JavaScriptInterface {
public int incrementNumber(String num) {
// Leaving out try/catch
int tempNum = newRadius = Integer.parseInt(num);
return tempNum + 1;
}
... everything seems to work. So I am wondering if JavaScriptInterface method arguments should/can only be of type String?
Relevant resources:
http://developer.android.com/reference/android/webkit/WebView.html
http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface(java.lang.Object, java.lang.String)
http://code.google.com/apis/maps/articles/android_v3.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在 Java 端要求 String args 或确保数字是实际数字(而不是数字的文本版本 - 请参阅 about.com - JavaScript:字符串到数字)在 JavaScript 端。
You can either require String args on the Java side or ensure that numbers are actual numbers (and not text versions of numbers - see about.com - JavaScript: Strings to Numbers) on the JavaScript side.
唯一相关的官方文档在这里:
http://developer.android.com/guide/webapps/webview.html
但没有关于可用类型的描述
The only relevant official doc is here:
http://developer.android.com/guide/webapps/webview.html
But no description about the available types