Closed
Description
iOS
// Add the notification observer
if (app.ios) {
app.ios.addNotificationObserver(UIDeviceBatteryLevelDidChangeNotification,
function onReceiveCallback(notification: NSNotification) {
var percent = UIDevice.currentDevice().batteryLevel * 100;
var message = "Battery: " + percent + "%";
console.log(message);
});
}
// When no longer needed, remove the notification observer
if (app.ios) {
app.ios.removeNotificationObserver(UIDeviceBatteryLevelDidChangeNotification);
}
Android
//Register the broadcast receiver
if (app.android) {
app.android.registerBroadcastReceiver(android.content.Intent.ACTION_BATTERY_CHANGED,
function onReceiveCallback(context: android.content.Context, intent: android.content.Intent) {
var level = intent.getIntExtra(android.os.BatteryManager.EXTRA_LEVEL, -1);
var scale = intent.getIntExtra(android.os.BatteryManager.EXTRA_SCALE, -1);
var percent = (level / scale) * 100.0;
console.log("Battery: " + percent + "%");
});
}
// When no longer needed, unregister the broadcast receiver
if (app.android) {
app.android.unregisterBroadcastReceiver(android.content.Intent.ACTION_BATTERY_CHANGED);
}