Raviprakash
Raviprakash

Reputation: 2440

How to get battery status?

How do I get the battery status on an iPhone?

Upvotes: 16

Views: 12854

Answers (5)

Has AlTaiar
Has AlTaiar

Reputation: 4152

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

Upvotes: 2

BadPirate
BadPirate

Reputation: 26177

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;
}

Upvotes: 0

Paresh Thakor
Paresh Thakor

Reputation: 1835

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.

Upvotes: 24

Grzegorz Adam Hankiewicz
Grzegorz Adam Hankiewicz

Reputation: 7661

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.

Upvotes: -2

Raviprakash
Raviprakash

Reputation: 2440

Iphone SDK 3.0 beta supports this.

Upvotes: 3

Related Questions