Android -- WebView solve the problem of using addJavascriptInterface In sdk 17

Source: Internet
Author: User

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_MR1And above, only public methods that are annotatedJavascriptInterfaceCan be accessed from JavaScript. For applications targeted to API levelJELLY_BEANOr 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 levelJELLY_BEANOr 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.