监控iPhone的电池状态

来源:互联网 发布:ubuntu安装ruby 编辑:程序博客网 时间:2024/05/01 10:28

 

 

-(void) checkBattery:(id)sender{

          NSArray *stateArray=[NsArray arrayWithObjects: @"Battery state is Unknown",

           @"Battery is unplugged",@"Battery is charging",@"Battery state is full",nil];

 

         NsLog(" Battery level:%0.2f%",[[uiDevice currentDevice] batteryLevel] * 100);

         NSLog("Battery state:%@",[stateArray objectAtIndex:[[UIDevice currentDevice] batteryState])

 

}

 

-(void) viewDidLoad{

   [   [UIDevice currentDevice] setBatteryMonitoringEnabled:YES];

  

   [ [ NSNotificationCenter defaultCenter ] addObserver:self selector:@selector(checkBattery)

                name:UIDeviceBatteryStateDidChangeNotification

                 object:nil   ];

 

[ [ NSNotificationCenter defaultCenter ] addObserver:self selector:@selector(checkBattery)

                name:UIDeviceBatteryLevelDidChangeNotification

                 object:nil   ];

 

 

}

原创粉丝点击