When addJavascriptInterface of WebView is called, there is no problem when android: targetSdkVersion = "10" is used and the event can be triggered. However, after all, the latest version is used, during development, I used the android: targetSdkVersion = "17" attribute to pursue new things. It was no problem when I started using it. Most mobile phones can use it, for example, in ZTE's N986 and Xiaomi's MIMU4.1 systems, there was no problem, and there were no problems with several models whose systems were 2.3. Later, there was a problem with Samsung Note3 and it could not be called, I also struggled for a long time. Later, I found the cause on the Internet, because of last year's android vulnerability,
Therefore, there are two ways to modify it:
1. Modify android: targetSdkVersion = "10". This is only a temporary solution,
2. Search for the official file: It is said that you need to add an interface named JavascriptInterface for use above 17. Later, after carefully reading the official Demo, you can find the interface, which is the blue bold part. If you have encountered this problem, I hope to help you. Thank you.
Official Instructions:
Public void addJavascriptInterface (Object object, String name) Added in API level 1
Injects the supplied Java object into this WebView. the object is 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 levelJELLY_BEAN_MR1
And above, only public methods that are annotatedJavascriptInterface
Can be accessed from JavaScript. For applications targeted to API levelJELLY_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 is next (re) loaded. For example:
class JsObject { @JavascriptInterface public String toString() { return "injectedObject"; } } webView.addJavascriptInterface(new JsObject(), "injectedObject"); webView.loadData("", "text/html", null); webView.loadUrl("javascript:alert(injectedObject.toString())");
IMPORTANT:
- 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 applications targeted to API level
JELLY_BEAN
Or below, because JavaScript cocould use reflection to access an injected object's public fields. use of this method in a WebView containing untrusted content cocould 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 cocould contain untrusted content.
- JavaScript interacts with Java object on a private, background thread of this WebView. Care is therefore required to maintain thread safety.
- The Java object's fields are not accessible.Parameters
Object |
The Java object to inject into this WebView's JavaScript context. Null values are ignored. |
Name |
The name used to expose the object in JavaScript |