diff --git a/docs/html/guide/webapps/webview.jd b/docs/html/guide/webapps/webview.jd index 66b5501688204..bdedefffe922d 100644 --- a/docs/html/guide/webapps/webview.jd +++ b/docs/html/guide/webapps/webview.jd @@ -158,7 +158,7 @@ access the class.
For example, you can include the following class in your Android application:
-public class JavaScriptInterface {
+public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
@@ -167,13 +167,23 @@ public class JavaScriptInterface {
}
/** Show a toast from the web page */
+ @JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
-In this example, the {@code JavaScriptInterface} class allows the web page to create a {@link +
Caution: If you've set either your {@code minSdkVersion} or {@code targetSdkVersion} +to 17 or higher, you +must add the {@code @JavascriptInterface} annotation to any method that you want +available your web page code (the method must also be public). If you do not provide the +annotation, then the method will not accessible by your web page when running on Android 4.2 or +higher.
+ +In this example, the {@code WebAppInterface} class allows the web page to create a {@link android.widget.Toast} message, using the {@code showToast()} method.
You can bind this class to the JavaScript that runs in your {@link android.webkit.WebView} with @@ -182,12 +192,12 @@ name the interface {@code Android}. For example:
WebView webView = (WebView) findViewById(R.id.webview); -webView.addJavascriptInterface(new JavaScriptInterface(this), "Android"); +webView.addJavascriptInterface(new WebAppInterface(this), "Android");
This creates an interface called {@code Android} for JavaScript running in the {@link android.webkit.WebView}. At this point, your web application has access to the {@code -JavaScriptInterface} class. For example, here's some HTML and JavaScript that creates a toast +WebAppInterface} class. For example, here's some HTML and JavaScript that creates a toast message using the new interface when the user clicks a button:
@@ -203,7 +213,7 @@ message using the new interface when the user clicks a button:There's no need to initialize the {@code Android} interface from JavaScript. The {@link android.webkit.WebView} automatically makes it available to your web page. So, at the click of the button, the {@code showAndroidToast()} -function uses the {@code Android} interface to call the {@code JavaScriptInterface.showToast()} +function uses the {@code Android} interface to call the {@code WebAppInterface.showToast()} method.
Note: The object that is bound to your JavaScript runs in