当前电池状态始终返回"充电"在ios

时间:2014-09-04 13:03:11

标签: ios iphone batterylevel batterymanager

即使我的iPhone电池100%充电,我的电池状态仍为UIDeviceBatteryStateCharging

它应该给我状态为UIDeviceBatteryStateFull

我的代码如下。

[[UIDevice currentDevice]setBatteryMonitoringEnabled:YES];
    int i=[[UIDevice currentDevice] batteryState];
    switch (i)
    {
        case UIDeviceBatteryStateUnplugged:
        {
            NSLog(@"UnpluggedKey");

            break;
        }
        case UIDeviceBatteryStateFull:
        {
            NSLog(@"FullKey");

            break;
        }
        case UIDeviceBatteryStateCharging:
        {
            NSLog(@"ChargingKey");
            break;
        }
        default:
        {
            NSLog(@"UnknownKey");
            break;
        }
    }

    NSLog(@"Battery Status is :%d",i);

2 个答案:

答案 0 :(得分:2)

它可能有助于你尝试这个......

- (void)viewDidLoad
{
// Enable monitoring of battery status
    [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];

    // Print current status
    [self batteryStatus];

    // Request to be notified when battery charge or state changes
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryStatus) name:UIDeviceBatteryLevelDidChangeNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryStatus) name:UIDeviceBatteryStateDidChangeNotification object:nil];

}
 - (void)batteryStatus
{
 NSArray *batteryStatus = [NSArray arrayWithObjects: 
    @"Battery status is unknown.", 
    @"Battery is in use (discharging).", 
    @"Battery is charging.", 
    @"Battery is fully charged.", nil];

    if ([[UIDevice currentDevice] batteryState] == UIDeviceBatteryStateUnknown)
  {
        NSLog(@"%@", [batteryStatus objectAtIndex:0]);
  }
    else
  {     
    NSString *msg = [NSString stringWithFormat:
                                        @"Battery charge level: %0.2f%%\n%@", [[UIDevice currentDevice] batteryLevel] * 100,
                      [batteryStatus objectAtIndex:[[UIDevice currentDevice] batteryState]] ];
        NSLog(@"%@", msg);
    }
}

你的回答是......

您正在使用不同的枚举键值

进行检查
typedef NS_ENUM(NSInteger, UIDeviceBatteryState) {
    UIDeviceBatteryStateUnknown,
    UIDeviceBatteryStateUnplugged,   // on battery, discharging
    UIDeviceBatteryStateCharging,    // plugged in, less than 100%
    UIDeviceBatteryStateFull,        // plugged in, at 100%
};              // available in iPhone 3.0

像这样改变你的代码它会起作用

[[UIDevice currentDevice]setBatteryMonitoringEnabled:YES];
    int i=[[UIDevice currentDevice] batteryState];
    switch (i)
    {
        case UIDeviceBatteryStateUnplugged:
        {
            NSLog(@"UnpluggedKey");

            break;
        }
        case UIDeviceBatteryStateCharging:
        {
            NSLog(@"ChargingKey");
            break;
        }
        case UIDeviceBatteryStateFull:
        {
            NSLog(@"FullKey");

            break;
        }

        default:
        {
            NSLog(@"UnknownKey");
            break;
        }
    }

    NSLog(@"Battery Status is :%d",i);

我希望它能帮到你

答案 1 :(得分:0)

ADC应用程序表现相同......从不显示FULL。

https://developer.apple.com/reference/uikit/uidevicebatteryleveldidchangenotification

所以Larme似乎是对的。