Add a PhoneGap plug-in to the BlackBerry Hybrid Web Container
This example procedure shows the steps to create and use a custom plug-in to get battery information for the device.
< plugin name="Battery1" value="com.sybase.hwc.Battery1"/>
package com.sybase.hwc; import org.apache.cordova.api.Plugin; import org.apache.cordova.api.PluginResult; import org.apache.cordova.json4j.JSONArray; public class Battery1 extends Plugin { public static final String GET_LEVEL = "getLevel"; /** * Executes the requested action and returns a PluginResult. * * @param action The action to execute. * @param callbackId The callback ID to be invoked upon action completion. * @param args JSONArry of arguments for the action. * @return A PluginResult object with a status and message. */ public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult result = null; if (GET_LEVEL.equals(action)) { // retrieve the device battery level int level = net.rim.device.api.system.DeviceInfo.getBatteryLevel(); result = new PluginResult(PluginResult.Status.OK, level); } else { result = new PluginResult(PluginResult.Status.INVALID_ACTION, "Battery: Invalid action: " + action); } return result; } /** * Called when Plugin is paused. */ public void onPause() { } /** * Called when Plugin is resumed. */ public void onResume() { } /** * Called when Plugin is destroyed. */ public void onDestroy() { } }
var Battery1 = { level: function(successCallback, errorCallback) { PhoneGap.exec(successCallback, errorCallback, 'Battery1', 'getLevel',[]); } }; function getBatteryLevel() { Battery1.level(function(level) { alert('Battery level is ' + level); }, function(error) { alert('Error retrieving battery level:' + error); }); }
function customAfterHybridAppLoad() { document.addEventListener("deviceready", getBatteryLevel, false ); }