appView.addJavascriptInterface() does not work on API 17

前端 未结 4 2058
我寻月下人不归
我寻月下人不归 2021-02-01 05:27

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 fr

相关标签:
4条回答
  • 2021-02-01 06:22

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

    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?

    objCustomNativeAccess = new CustomNativeAccess(this, appView);
    appView.addJavascriptInterface(objCustomNativeAccess, "CustomNativeAccess");
    appView.loadUrl("file:///android_asset/www/index.html");
    
    0 讨论(0)
  • 2021-02-01 06:27

    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.

    0 讨论(0)
  • 2021-02-01 06:27

    Solutions:

    1. Lower the android:targetSdkVersion in the manifest file to 16.
    2. Change the compilation api level to 17 in project.properties: target=android-17 and annotate the callback method with @JavascriptInterface.
    0 讨论(0)
  • 2021-02-01 06:29

    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)

    0 讨论(0)
提交回复
热议问题