appView.addJavascriptInterface() does not work on API 17
Solution 1
What you have to do on API 17 is annotate your method with @JavascriptInterface
:
public class CustomNativeAccess {
@JavascriptInterface
and then get rid of the constructor part:
/*private WebView mAppView;
private DroidGap mGap;
public CustomNativeAccess(DroidGap gap, WebView view) {
mAppView = view;
mGap = gap;
}
*/
Also be sure you import JavascriptInterface in your project:
import android.webkit.JavascriptInterface;
You can read about it more here: WebView Android
Edit: You will have to annotate each method with @JavascriptInterface within your class that you'd like to access from Javascript.
Solution 2
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.
Source: Android WebView Doc (emphasis added)
vivek tiwari
Android developer at Retail Reco pvt. ltd. Indore http://www.retailreco.com/
Updated on July 09, 2022Comments
-
vivek tiwari almost 2 years
i am able to use java function from my phonegap java script function and android 2.2 but same code is not run on API 17. what should i have to do to call native java code on from java script in API 17.
i use this code in my java file
objCustomNativeAccess = new CustomNativeAccess(this, appView); appView.addJavascriptInterface(objCustomNativeAccess, "CustomNativeAccess"); super.loadUrl("file:///android_asset/www/index.html");
my CustomNativeAccess class is
public class CustomNativeAccess { private WebView mAppView; private DroidGap mGap; /** * Constructor * * @param gap * @param view */ public CustomNativeAccess(DroidGap gap, WebView view) { mAppView = view; mGap = gap; } /** * Get the device phone number * * @return */ public JSONObject login(String email, String password) { JSONObject object = new JSONObject(); object.put("Login_Status", login_status); object.put("date", dateString); return object; }
and in my java script i use this line to call this login function
var value = window.CustomNativeAccess.login(email,pass);
so using this i successfully call this on api 2.2 but when i run this code on api 17 it give me error
Uncaught TypeError: Object [object Object] has no method 'login' at file:///android_asset/www/index.html:81
how i can i use this on api 17
-
gregory561 almost 11 yearsOne can use constructors that is not a problem.
-
benka over 10 yearsno probs with the constructors, but it's not necessary at that point and
-
Akhil Jain over 10 years
appView.setJavaScriptEnabled
does not exist , whichPhoneGap version
did you used?? i am using version2.9
-
buptcoder over 10 yearssorry, there is a mistake. it should be
webSettings.setJavaScriptEnabled(true).
And I never use PhoneGap. -
nacho4d almost 8 yearsI can confirm that there is no problem with the constructor. The conditions are methods must have the annotation @JavascriptInterface and be public.
-
KaKa almost 8 yearsshould add
@SuppressLint( "AddJavascriptInterface")
and@JavascriptInterface
in my case. -
android developer about 5 yearsIt is said that this is for security: "Any untrusted content hosted in a WebView could potentially use reflection to figure out the public methods within the JavascriptInterface object and could then make use of them." ( android-developers.googleblog.com/2013/02/… ) . This is only of the object that has it right? It can't reach further? If I have an anonymous class for it, and it has a single function (and it has this annotation), is it dangerous?
-
Ranjithkumar over 2 yearsIt's outdated answer. Play store will not allow to set targetsdk=17. FYI