How to get battery status?

How do I get the battery status on an iPhone?

Answers


UIDevice *myDevice = [UIDevice currentDevice];
[myDevice setBatteryMonitoringEnabled:YES];
float batLeft = [myDevice batteryLevel];
int i=[myDevice batteryState];

int batinfo=(batLeft*100);

NSLog(@"Battry Level is :%d and Battery Status is :%d",batinfo,i);

switch (i)
{
    case UIDeviceBatteryStateUnplugged:
    {
        [BCStatus setText:NSLocalizedString(@"UnpluggedKey", @"")];
        break;
    }
    case UIDeviceBatteryStateCharging:
    {
        [BCStatus setText:NSLocalizedString(@"ChargingKey", @"")];
        break;
    }
    case UIDeviceBatteryStateFull:
    {
        [BCStatus setText:NSLocalizedString(@"FullKey", @"")];
        break;
    }
    default:
    {
        [BCStatus setText:NSLocalizedString(@"UnknownKey", @"")];
        break;
    }
}

BCStatus is uilabel.


Iphone SDK 3.0 beta supports this.


The answers above are very good, but they are all in Obj-C, I have used these with other examples to do the same task on MonoTouch, so I am putting my code here in case anybody needs it:

try
{
    UIDevice.CurrentDevice.BatteryMonitoringEnabled = true;
    _Battery.Level = (int)(UIDevice.CurrentDevice.BatteryLevel * IOSBatteryLevelScalingFactor);
    _Battery.State = UIDevice.CurrentDevice.BatteryState;
}
catch (Exception e)
{
    ExceptionHandler.HandleException(e, "BatteryState.Update");
    throw new BatteryUpdateException();
}
finally
{
    UIDevice.CurrentDevice.BatteryMonitoringEnabled = false;
}

I also have a full post on my blog to give all the details in here


Here's what I used for my string as a quick utility method, note that you have to enable battery monitoring to get a value, and then if you don't want to get the notifications (obviously some efficiency to be gained there, since they give you the ability to turn it off) then you should turn it off again after (like I do in this function):

NSString *statusString(void)
{
    UIDevice *device = [UIDevice currentDevice];
    NSString *batteryStateString = nil;
    switch(device.batteryState)
    {
        case UIDeviceBatteryStateUnplugged: batteryStateString = @"Unplugged"; break;
        case UIDeviceBatteryStateCharging: batteryStateString = @"Charging"; break;
        case UIDeviceBatteryStateFull: batteryStateString = @"Full"; break;
        default: batteryStateString = @"Unknown"; break;
    }

    [device setBatteryMonitoringEnabled:YES];
    NSString *statusString = [NSString stringWithFormat:@"Battery Level - %d%%, Battery State - %@",
                              (int)round(device.batteryLevel * 100), batteryStateString];
    [device setBatteryMonitoringEnabled:NO];
    return statusString;
}

Now that the 3.1 SDK is released look for the Getting the Device Battery State section in UIDevice's documentation. It is abunch of battery* properties.


Need Your Help

Php. Number of zeros in float

php format

I have to send the JSON:

Linux IRQ: unmasking IRQ within ISR

linux-kernel isr softirq

I have an IRQ which is using handle_level_irq(). Most of the time, the ISR requires that a bottom half be scheduled, but occasionally, it is able to determine it is spurious, and does not want to