-2
我在执行中,我想通过编程来读取iPhone的电池状态一个iPhone应用程序。我不知道这是否可能。请给我建议。
我在执行中,我想通过编程来读取iPhone的电池状态一个iPhone应用程序。我不知道这是否可能。请给我建议。
读取电池电量和状态,你只需要:
float battLvl = [[UIDevice currentDevice] batteryLevel]; // range is from 0.0 to 1.0
UIDeviceBatteryState battState = [UIDevice currentDevice] batteryStatus];
监测电池的使用水平和状态,你可以做到以下几点:
// enable monitoring and add observers with selector
UIDevice *device = [UIDevice currentDevice];
device.batteryMonitoringEnabled = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryChanged:) name:@"UIDeviceBatteryLevelDidChangeNotification" object:device];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryChanged:) name:@"UIDeviceBatteryStateDidChangeNotification" object:device];
// Your notification handler
- (void)batteryChanged:(NSNotification *)notification
{
UIDevice *device = [UIDevice currentDevice];
NSLog(@"state: %i | charge: %f", device.batteryState, device.batteryLevel);
}
的代码下面几行会有所帮助:
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
[[UIDevice currentDevice] batteryLevel];
[[UIDevice currentDevice] batteryState];
有关详细信息,请参阅THIS链接。它会详细说明你。