-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鏈接。它會詳細說明你。