How to call native function from cordova 3.x

后端 未结 1 667
谎友^
谎友^ 2020-12-18 18:02

How can I call a native function from cordova/phonegap webview for example for showing an ad.

EDIT: OK I FUNALLY GOT IT and I\'m gonna write some steps for all of y

相关标签:
1条回答
  • 2020-12-18 18:18

    In the .java file

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        super.init(); // Calling this is necessary to make this work
    
        appView.addJavascriptInterface(this, "MainActivity");
    
        super.loadUrl(Config.getStartUrl());
    
    }
    

    In the javascript

    window.MainActivity.customFunctionCalled();
    

    This will only work on TargetSDK<17 . An androidManifest.xml targetSDK need to be set < 17. And for TargetSDK >=17 , I guess you'll need to create some custom plugin. I use this process lowering my target SDK for now.

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