iOS开发:电池电量监测

一、电池状态获取及监测

#pragma mark - 电池状态获取及监控
-(void)checkAndMonitorBatteryState{
    
    UIDevice * device = [UIDevice currentDevice];
    //是否允许监测电池
    //要想获取电池状态和监控电池状态 必须允许
    device.batteryMonitoringEnabled = true;
    //1、check
    /*
     电池状态
     typedef NS_ENUM(NSInteger, UIDeviceBatteryState) {
     UIDeviceBatteryStateUnknown,
     UIDeviceBatteryStateUnplugged,   // on battery, discharging
     UIDeviceBatteryStateCharging,    // plugged in, less than 100%
     UIDeviceBatteryStateFull,        // plugged in, at 100%
     } __TVOS_PROHIBITED;
     */
    UIDeviceBatteryState state = device.batteryState;
    NSArray *stateArray = [NSArray arrayWithObjects:@"未开启监视电池状态",@"电池未充电状态",@"电池充电状态",@"电池充电完成",nil];
    NSLog(@"电池状态:%@", [stateArray objectAtIndex:state]);
    
    //2、monitor
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChangBatteryState:) name:@"UIDeviceBatteryStateDidChangeNotification" object:device];
}

-(void)didChangBatteryState:(NSNotification *)notification{
    //电池状态发生改变时调用
    
}

二、电池电量获取及监测

#pragma mark - 电池电量获取及监控
-(void)checkAndMonitorBatteryLevel{
    
    //拿到当前设备
    UIDevice * device = [UIDevice currentDevice];
    
    //是否允许监测电池
    //要想获取电池电量信息和监控电池电量 必须允许
    device.batteryMonitoringEnabled = true;
    
    //1、check
    /*
     获取电池电量
     0 .. 1.0. -1.0 if UIDeviceBatteryStateUnknown
     */
    float level = device.batteryLevel;
    NSLog(@"level = %lf",level);
    
    //2、monitor
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChangeBatteryLevel:) name:@"UIDeviceBatteryLevelDidChangeNotification" object:device];
    
}

- (void)didChangeBatteryLevel:(id)sender{
    //电池电量发生改变时调用
    UIDevice *myDevice = [UIDevice currentDevice];
    [myDevice setBatteryMonitoringEnabled:YES];
    float batteryLevel = [myDevice batteryLevel];
    NSLog(@"电池剩余比例:%@", [NSString stringWithFormat:@"%f",batteryLevel*100]);
}

三、低电量模式切换监测

#pragma mark - 低电量模式切换
-(void)checkAndMonitorPowerMode{
    //1、check
    //是否处于低电量模式
    if ([[NSProcessInfo processInfo] isLowPowerModeEnabled]) {
        NSLog(@"处在低电量模式");
    }
    else{
        NSLog(@"未处于低电量模式");
    }
    
    //2、monitor
    //低电量模式切换通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didChangePowerMode:)
                                                 name:NSProcessInfoPowerStateDidChangeNotification
                                               object:nil];

}

//收到低电量通知之后调用的方法
//PS:手动设置低电量模式时,程序会回到后台,当程序从后台回到前台时就会调用该方法
- (void)didChangePowerMode:(NSNotification *)notification {
    if ([[NSProcessInfo processInfo] isLowPowerModeEnabled]) {
        NSLog(@"打开低电量模式");
    } else {
        NSLog(@"关闭低电量模式");
    }
}

参考文章:

1、获取IOS设备的电量信息:Battery Level - iPhone手机开发技术文章 - 红黑联盟

2、[iOS] 如何检测 iPhone 处于低电量模式 - 推酷

3、IOS 电池状态监控 - 永远在跟随,从未去超越 - 博客频道 - CSDN.NET

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值