Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webview with JavaScript interface

I am building a mobile application which should be interacted from java to javascript.

I found the solution in this link Passing an array of values from Android Activity to JavaScript in a WebView. But my problem is when i declare webview its showing error like

None of the methods in the added interface have been annotated with @android.webkit.JavascriptInterface; they will not be visible in API 17

What should I do to remove this error? I am very new to this core java. Please suggest me and help me to solve this problem.

like image 415
user3335796 Avatar asked Mar 06 '14 10:03

user3335796


People also ask

Does WebView support 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.

What is JavascriptInterface Android?

android.webkit.JavascriptInterface. Annotation that allows exposing methods to JavaScript. Starting from API level Build. VERSION_CODES. JELLY_BEAN_MR1 and above, only methods explicitly marked with this annotation are available to the Javascript code.

Can we use JavaScript in Android Studio?

Can we use JavaScript for Android? Yes, of course! The Android ecosystem supports the concept of hybrid apps, which is a wrapper over the native platform. It mimics the UI, UX, and all kinds of hardware and network interactions, just like how you would use a native Android app.

Which browser does WebView use?

A WebView is an embeddable browser that a native application can use to display web content while a web app provides additional functionality and interactivity. Web apps load in browsers like Chrome or Safari and do not take up any storage on the user's device.


1 Answers

From the docs:

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.

Please take a look here http://developer.android.com/guide/webapps/webview.html

like image 141
nikis Avatar answered Oct 07 '22 09:10

nikis