Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebView JavaScript Bridge Documentation

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

like image 499
tamsler Avatar asked Nov 06 '22 03:11

tamsler


1 Answers

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.

like image 155
typo.pl Avatar answered Nov 15 '22 12:11

typo.pl