2013-05-03 114 views
21

我能够使用我的phonegap java脚本函数和android 2.2 中的java函数,但是相同的代码不在API 17上运行。我应该怎么做才能调用native从Java的Java脚本API中对代码17appView.addJavascriptInterface()不适用于API 17

我在Java文件中使用此代码

objCustomNativeAccess = new CustomNativeAccess(this, appView); 
      appView.addJavascriptInterface(objCustomNativeAccess, 
        "CustomNativeAccess"); 
    super.loadUrl("file:///android_asset/www/index.html"); 

我CustomNativeAccess类是

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; 
     } 

和在我的Java脚本我使用此行来调用这个登录功能

var value = window.CustomNativeAccess.login(email,pass); 

,因此使用此我成功地把这种对API的2.2,但是当我运行API 17这段代码把它给我错误

遗漏的类型错误:对象的翻译:有没有方法“登录”的文件:///android_asset/www/index.html:81

我如何我使用这个API上17

回答

38

你有什么对API 17要做的就是用@JavascriptInterface注解你的方法:

Public class CustomNativeAccess { 
    @JavascriptInterface 

,然后摆脱构造部分:

/*private WebView mAppView; 
    private DroidGap mGap; 
    public CustomNativeAccess(DroidGap gap, WebView view) { 
     mAppView = view; 
     mGap = gap; 
    } 
*/ 

还要确保你在您的项目中导入JavascriptInterface:

import android.webkit.JavascriptInterface; 

您可以在这里更多的了解它: http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface%28java.lang.Object,%20java.lang.String%29

编辑:你必须标注每一个方法与你的类中@JavascriptInterface,你想从JavaScript访问。

+1

可以使用不是问题的构造函数。 – gregory561 2013-08-07 12:47:07

+1

no probs与构造函数,但它没有必要在这一点, – benka 2013-10-30 14:15:49

+0

我可以证实,构造函数没有问题。条件是方法必须有注解** @ JavascriptInterface **并且是** public **。 – nacho4d 2016-06-22 07:04:35

0

我刚写的Web应用程序与使用API​​ 17的addJavascriptInterface,它运行良好。我不知道你的代码发生了什么。也许你可以试试这些:

确保你添加:

WebSettings webSettings = appView.getSettings(); 
webSettings.setJavaScriptEnabled(true); 

二,为什么要使用super.loadUrl,不APPVIEW加载HTML?

objCustomNativeAccess = new CustomNativeAccess(this, appView); 
appView.addJavascriptInterface(objCustomNativeAccess, "CustomNativeAccess"); 
appView.loadUrl("file:///android_asset/www/index.html"); 
+0

'appView.setJavaScriptEnabled' does not exist,which'PhoneGap version' did you used ???我正在使用版本'2.9' – 2014-02-16 08:18:36

+0

对不起,有一个错误。它应该是'webSettings.setJavaScriptEnabled(true).'我从不使用PhoneGap。 – buptcoder 2014-02-17 10:58:19

4

从Android 4.2的文档:

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.

来源:机器人的WebView文件(强调)

0

解决方案:

  1. 下在清单文件中的android:targetSdkVersion至16
  2. 在project.properties中将编译api级别更改为17:target=android-17并注释回调方法与@JavascriptInterface
相关问题