You can create your own plugin to call any method from the inside. Create a separate JavaScript file, say customplugin.js, and put it in it:
var CustomPlugin = {}; CustomPlugin.callNativeMethod = function() { cordova.exec(null, null, "CustomPlugin", "callNativeMethod", []); };
Now on the side of native Java, create a new class and name it CustomPlugin.java, then add the following:
package com.yourpackage; import org.apache.cordova.CordovaWebView; import org.apache.cordova.api.CallbackContext; import org.apache.cordova.api.CordovaInterface; import org.apache.cordova.api.CordovaPlugin; import com.yourpackage.MainActivity; public class CustomPlugin extends CordovaPlugin { private static final String TAG = "CustomPlugin"; private CallbackContext callbackContext = null; private MainActivity activity = null; @Override public void initialize(CordovaInterface cordova, CordovaWebView webView) { super.initialize(cordova, webView);
Make sure you insert the plugins into the config.xml file by adding this line:
... <feature name="CustomPlugin"> <param name="android-package" value="com.yourpackage.CustomPlugin" /> </feature> ...
Now, to call the plugin from your index.html, you can simply call your JavaScript method:
CustomPlugin.callNativeMethod();
Using this method will allow you to configure many custom methods. For more information, see the PhoneGap Plugin Development Guide here .
kieranroneill
source share