Android - Implementing Apache Cordova plugin
In some cases, developers of a MobileFirst application might have to use a specific third-party native library or a device function that is not yet available in Apache Cordova.
Note: In Cordova-based applications, developers must check for the
deviceready event before they use the Cordova API set. In a MobileFirst application, however, this check is done internally.
Instead of implementing this check, you can place implementation code in the
wlCommonInit() function in
- Creating a plug-in
- Declaring a plug-in
- Implementing the Java code of a Cordova plug-in
- Sample application
Creating a plug-in
- Declare the plug-in in the
- Use the
- Create the plug-in class that will run natively in Android.
Declaring a plug-in
You must declare the plug-in in the project, so that Cordova can detect it.
To declare the plug-in, add a reference to the
config.xml file, located in the
native\res\xml folder in the Android environment.
cordova.exec() method to call the Cordova plug-in:
sayHelloSuccess - Success callback
sayHelloFailure - Failure callback
SayHelloPlugin - Plug-in name as declared in
sayHello - Action name
[name] - Parameters array
The plug-in calls the
Implementing the Java code of a Cordova plug-in
- Add a new Java class file.
- Extend the
org.apache.cordova.CordovaPluginclass and add the required import statements.
- Implement an
- The arguments contain information that is required by a plug-in, such as action, arguments array, and callback context.
- If the supplied action is
sayHello, retrieve the first argument from the
argsarray, prepare a
responseTextstring and, by using the
callbackContextargument, call the
successcallback with this
responseTextstring as the argument.
Click to download the Studio project.▲