WebView使用中jsがjavaコードを呼び出した場合targetSdkVarsionが17より大きい場合、正常に呼び出せません


開発の過程でwebviewを使って1つのネットのピクチャーをロードしてそしてピクチャーをクリックする時相応の動作があって、webviewがすべての動作のイベントをキャプチャしたため、jsを使ってjavaのローカルコードの方式を呼び出して需要を完成して、使用する時、use-sdkの中でandroidを配置することを発見します:targetSdkVarsionが17より大きい時、jsはjavaのコードを呼び出すことができなくて、
修正方法は2つあります.
       1.修正android:targetSdkValerssion="10"これは一時的な解決策にすぎず、
       2. 公式ファイルの検索:jsで呼び出されたjavaコードにJavascriptInterfaceの注釈を付けるインタフェースJavascriptInterfaceを17以上追加する必要があります.また、コードを混同する場合は、JavascriptInterfaceの注釈を混同しないように注意してください.そうしないと、jsがjavaコードを呼び出すことができない場合もあります.(-keepattributes *Annotation*)
 
公式の説明:
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:
?
1
2
3
4
5
6
7 class JsObject {    <strong> @JavascriptInterface </strong>    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

  •