ios - 当前电池状态总是在 ios 中返回 "Charging"

标签 ios iphone batterylevel batterymanager

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

它应该给我的状态为 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);

最佳答案

它可能会帮助你尝试这个......

- (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);

希望对你有帮助

关于ios - 当前电池状态总是在 ios 中返回 "Charging",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25666270/

相关文章:

macos - 在 Mac OSX 上获取蓝牙耳机的电池电量

ios - UITableView - BeginUpdates/EndUpdates 动画设置

iphone - 如何突出显示字符串的NSRange

iOS:从 url 加载图像

iphone - 使用 UIActivityIndi​​catorView 覆盖 View 的示例

iphone - UILabels 完整文本不可见

ios - 转到另一个 Storyboard,但导航 Controller 仍然可见

ios swift : How can i delay cell to appear in a tableview?

给笔记本电池充放电的Python脚本

Android 持久套接字连接规则