public void addJavascriptInterface (Objectobject, String name) Added in API level 1
Injects the supplied Java objectinto this WebView. The objectis injected into the JavaScript context of the main frame, using the supplied name. This allows the Java object's methods to be accessed from JavaScript. For applications targeted to API level JELLY_BEAN_MR1 and above, only public methods that are annotated with JavascriptInterface can be accessed from JavaScript. For applications targeted to API level JELLY_BEAN or below, all public methods (including the inherited ones) can be accessed, see the important security note below for implications.
Note that injected objects will not appear in JavaScript until the page isnext (re)loaded. For example:
1 2 3 4 5 6 7 8 9
classJsObject { @JavascriptInterface public String toString() { return"injectedObject"; } }
This method can be used to allow JavaScript to control the host application. This is a powerful feature, but also presents a security risk for apps targeting JELLY_BEAN or earlier. Apps that target a version later than JELLY_BEAN are still vulnerable if the app runs on a device running Android earlier than 4.2. The most secure way to use this method isto target JELLY_BEAN_MR1 andto ensure the method is called only when running on Android 4.2or later. With these older versions, JavaScript could use reflection to access an injected object's public fields. Use of this method in a WebView containing untrusted content could allow an attacker to manipulate the host application in unintended ways, executing Java code with the permissions of the host application. Use extreme care when using this method in a WebView which could contain untrusted content. JavaScript interacts with Java objecton a private, background thread of this WebView. Care is therefore required to maintain thread safety.
The Java object's fields are not accessible. For applications targeted to API level LOLLIPOP and above, methods of injected Java objects are enumerable from JavaScript. Parameters object the Java object to inject into this WebView's JavaScript context. Null values are ignored. name the name used to expose the objectin JavaScript
Give the host application a chance to take over the control when a new url is about to be loaded in the current WebView. If WebViewClient isnot provided, bydefault WebView will ask Activity Manager to choose the proper handlerfor the url. If WebViewClient is provided, returntrue means the host application handles the url, whilereturnfalse means the current WebView handles the url. This methodisnotcalledfor requests using the POST "method".