appView.addJavascriptInterface()不能在API 17个工作能在、工作、addJavascriptInterface、appView

2023-09-04 09:03:42 作者:我有一颗坚定不移的心

我能够使用Java函数从我的PhoneGap Java脚本功能和Android 2.2 但同样的code未在API 17.我应该怎么要做调用本地Java code从Java脚本API中运行17

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.

我用这个code。在我的Java文件

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");

我CustomNativeAccess类

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

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

and in my java script i use this line to call this login function

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

因此​​采用这种我成功地把这种对API 2.2,但是当我运行的API 17本code它给我的错误

so using this i successfully call this on api 2.2 but when i run this code on api 17 it give me error

未捕获的类型错误:对象[对象的对象]有没有办法登陆的文件:///android_asset/www/index.html:81

Uncaught TypeError: Object [object Object] has no method 'login' at file:///android_asset/www/index.html:81

我我该如何使用这个API上17

how i can i use this on api 17

推荐答案

您所要做的API 17与@JavascriptInterface注释你的方法:

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

另外要确保你在你的项目中导入JavascriptInterface:

Also be sure you import JavascriptInterface in your project:

 import android.webkit.JavascriptInterface;

您可以阅读有关它的详细位置: http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface%28java.lang.Object,%20java.lang.String%29

You can read about it more here: http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface%28java.lang.Object,%20java.lang.String%29

编辑:你将不得不与@JavascriptInterface你的类中,你想从JavaScript访问注释每一个方法

You will have to annotate each method with @JavascriptInterface within your class that you'd like to access from Javascript.