我想在iOS中顯示我的設備的電池狀態。獲取ios中每次更改的電池狀態
我在寫下面的代碼來顯示電池狀態。
UIDevice *myDevice = [UIDevice currentDevice];
[myDevice setBatteryMonitoringEnabled:YES];
double batLeft = (float)[myDevice batteryLevel] * 100;
NSLog(@"%.f",batLeft);
NSString * levelLabel = [NSString stringWithFormat:@"%.f%%", batLeft];
lblLabel.text =levelLabel;
它在應用程序運行時顯示電池狀態正常。但是,當應用程序處於後臺時,它不會獲取更新後的值。我想每次顯示設備的電池狀態。電池在2%到3%之間死亡時,我也想發出通知。