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 stayed with a very basic code:
this.webView.getSettings().setJavaScriptEnabled(true); this.webView.setWebChromeClient(new WebChromeClient()); this.webView.addJavascriptInterface(new Object() { public void handshake() { Log.d("JS", "handshake no params"); } public void handshake(String json) { Log.d("JS", "handshake with params: " + json); } }, "Android");
In the javascript side of things the testing code looks like this:
Android.handshake();
But I get this in the logcat:
E/Web Console: Uncaught TypeError: Object [object Object] has no method 'handshake'
Again, this same exact thing works fine in devices who are have older android os (< 4.2.1) that I have tested (the minimal version being 2.3.3).
The strange thing is that if I start a completely new project, with nothing but a single Activity which has just a WebView, with the same code, everything works fine even for 4.2.1, but when it's part of my actual project, things break. There's nothing I'm doing with the webview which is not included in this code snippets I provided.
What is the most strange to me is that the javascript finds the Android object but it just does not have the requested method (handshake), how can that be?
Any help will be greatly appreciated since this has been driving me crazy for the past 2 weeks or so (it's a bug I go back to all the time, then give up, etc). Thanks.
Enable JavaScript JavaScript is disabled in a WebView by default. You can enable it through the WebSettings attached to your WebView . You can retrieve WebSettings with getSettings() , then enable JavaScript with setJavaScriptEnabled() . WebView myWebView = (WebView) findViewById(R.
Alternatives to WebView If you want to send users to a mobile site, build a progressive web app (PWA). If you want to display third-party web content, send an intent to installed web browsers. If you want to avoid leaving your app to open the browser, or if you want to customize the browser's UI, use Custom Tabs.
It was deprecated in API level 21.
Web view acts as an in-built browser which displays the webpages in Android. In other words, Web view turns your application into web application. WebView is an extension of Android's View class. It uses WebKit to display a web page which is a browser engine which provides tools for browsing the web.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With