android--WebView使用addJavascriptInterface在sdk 17的問題解決

來源:互聯網
上載者:User

標籤:des   android   style   class   c   code   

當調用WebView 的addJavascriptInterface時,使用android:targetSdkVersion="10"時是沒有問題的,能夠觸發事件,但是畢竟使用版本時一般都使用最新的,我在開發時為了追求新,然後使用了android:targetSdkVersion="17"的屬性,開始使用時並沒有什麼問題,大多數手機是可以使用的,比如中興的N986,小米的MIMU4.1的系統是沒有問題的,系統為2.3的幾個機型也沒看出問題,後來三星Note3上出問題啦,調用不了這個事件,我也糾結了半天,後來在網上尋找原因,是去年android的漏洞所致,

所以修改方法有兩個:

1,修改android:targetSdkVersion="10",這個只能為一時的解決方案,

2. 尋找官方檔案:說在17以上需要添加一個介面JavascriptInterface才能用,後來仔細看了官方的Demo才找到,就是藍色加粗部分。如果這個問題您也遇到過,希望能協助你,謝謝

官方給的說明:

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 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 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 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 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.