Android 4.2.1, WebView and javascript interface breaks

前端 未结 2 1764
鱼传尺愫
鱼传尺愫 2020-11-28 07:12

I have a webview with added javascript interface which works perfectly on most devices, except for those running Android 4.2.1.

I removed most of the code, and staye

相关标签:
2条回答
  • 2020-11-28 07:36

    You have to annotate (@JavascriptInterface) methods in Java class that you want to make available to JavaScript.

        public class JavaScriptInterface {
    Context context;
    
    
    JavaScriptInterface(Context c) {
        context = c;
    }
    
    @JavascriptInterface
    public void edit(String postid) {
        Log.d("myApp", "EDIT!");
        //do stuff
    }    }
    

    Its worked for me. Try out this.

    0 讨论(0)
  • 2020-11-28 07:48

    From the Android 4.2 documentation:

    Caution: If you've set your targetSdkVersion to 17 or higher, you must add the @JavascriptInterface annotation to any method that you want available your web page code (the method must also be public). If you do not provide the annotation, then the method will not accessible by your web page when running on Android 4.2 or higher.

    0 讨论(0)
提交回复
热议问题