appView.addJavascriptInterface() 不适用于 API 17

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16353430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:27:37  来源:igfitidea点击:

appView.addJavascriptInterface() does not work on API 17

javajavascriptandroidcordova

提问by vivek tiwari

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.

我可以使用我的 phonegap java 脚本函数和 android 2.2 中的 java 函数,但相同的代码没有在 API 17 上运行。我应该怎么做才能从 API 17 中的 java 脚本调用原生 java 代码。

i use this code in my java file

我在我的 java 文件中使用此代码

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

my CustomNativeAccess class is

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

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

在我的 java 脚本中,我使用这一行来调用这个登录函数

 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

所以使用这个我成功地在 api 2.2 上调用了这个但是当我在 api 17 上运行这个代码时它给了我错误

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

未捕获的类型错误:对象 [object Object] 在 file:///android_asset/www/index.html:81 没有方法“登录”

how i can i use this on api 17

我如何在 api 17 上使用它

采纳答案by benka

What you have to do on API 17 is annotate your method with @JavascriptInterface:

你在 API 17 上要做的是用 @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:

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

 import android.webkit.JavascriptInterface;

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

你可以在这里阅读更多信息:http: //developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface%28java.lang.Object,%20java.lang.String%29

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

编辑:您必须在要从 Javascript 访问的类中使用 @JavascriptInterface 注释每个方法。

回答by buptcoder

I just wrote the web application with addJavascriptInterfacewith API 17, it works well. I don't know what happened to your code. Maybe you can try these:

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

make sure you add the :

确保添加:

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

Second, why do you use super.loadUrl, not appView to load the html?

其次,为什么使用 super.loadUrl 而不是 appView 来加载 html?

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

回答by peter

From the Android 4.2 documentation:

来自 Android 4.2 文档:

Caution: If you've set your targetSdkVersion to 17 or higher, you must add the @JavascriptInterfaceannotation 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.

注意:如果您已将 targetSdkVersion 设置为 17 或更高,则必须将@JavascriptInterface注释添加到您希望在网页代码中可用的任何方法(该方法也必须是 public)。如果您不提供注释,则在 Android 4.2 或更高版本上运行时,您的网页将无法访问该方法。

Source: Android WebView Doc (emphasis added)

来源:Android WebView Doc(强调)

回答by AlikElzin-kilaka

Solutions:

解决方案:

  1. Lower the android:targetSdkVersionin the manifest file to 16.
  2. Change the compilation api level to 17 in project.properties: target=android-17and annotate the callback method with @JavascriptInterface.
  1. 放下android:targetSdkVersion清单文件16个。
  2. 在 project.properties: 中将编译 api 级别更改为 17,target=android-17并使用@JavascriptInterface.