In this recipe, we will learn how to read the charging states of the device and its battery usage.
Perform the following steps:
Add a label to the view of the controller.
Override the
ViewWillAppear
method in the controller class as follows:private NSObject batteryStateChangeObserver; public override void ViewWillAppear (bool animated) { base.ViewWillAppear (animated); UIDevice.CurrentDevice.BatteryMonitoringEnabled = true; this.batteryStateChangeObserver = UIDevice.Notifications.ObserveBatteryStateDidChange((s, e) => { this.lblOutput.Text = string.Format("Battery level: {0}", UIDevice.CurrentDevice.BatteryLevel); Console.WriteLine("Battery state: {0}", UIDevice.CurrentDevice.BatteryState); }); }
Compile and run the app on the device. After the app loads, disconnect and/or connect the USB cable of the device. The battery level will...