Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
194
rated 0 times [  197] [ 3]  / answers: 1 / hits: 30010  / 11 Years ago, fri, may 3, 2013, 12:00:00

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


More From » java

 Answers
31

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.


[#78456] Wednesday, May 1, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lailab

Total Points: 706
Total Questions: 102
Total Answers: 95

Location: Falkland Islands
Member since Mon, Jul 13, 2020
4 Years ago
;