#import "ViewController.h"

#import <UIKit/UIKit.h>

@interface ViewController ()




//@property(nonatomic,readonly) float     batteryLevel NS_AVAILABLE_IOS(3_0);


// 0 .. 1.0. -1.0 if UIDeviceBatteryStateUnknown它返回的是0.00-1.00之间的浮点值。

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    //打开电池的监听

    [UIDevice currentDevice].batteryMonitoringEnabled = YES;

    //获取电池的状态

    UIDeviceBatteryState BatteryState = [UIDevice currentDevice].batteryState;

    //获取剩余电量 范围在0.000000 1.000000之间

    CGFloat batterylevel = [UIDevice currentDevice].batteryLevel;

    //battery的状态分为:

    /*

     UIDeviceBatteryStateUnknown,     未知

     UIDeviceBatteryStateUnplugged,   // 未充电

     UIDeviceBatteryStateCharging,     // 正在充电

     UIDeviceBatteryStateFull,             // 满电

     

     */

    if (BatteryState == UIDeviceBatteryStateUnknown) {

        self.dlLabel.text = [NSString stringWithFormat:@"%0.0f%%",batterylevel*100];

        NSLog(@"unknow");

    }else{

        NSLog(@"know");

        //将剩余的电量用label显示。

        self.dlLabel.text = [NSString stringWithFormat:@"%0.0f%%",batterylevel*100];

    }

    

    

    

//    

//    [self getCurrentBatteryLevel];

//    [UIDevice currentDevice].batteryMonitoringEnabled = YES;

//    [[NSNotificationCenter defaultCenter]

//     addObserverForName:UIDeviceBatteryLevelDidChangeNotification

//     object:nil queue:[NSOperationQueue mainQueue]

//     usingBlock:^(NSNotification *notification) {

//         // Level has changed

//         dl=[UIDevice currentDevice].batteryLevel;

//         NSLog(@"电池电量:%.2f", [UIDevice currentDevice].batteryLevel);

//         

//         self.dlLabel.text = [NSString stringWithFormat:@"%f",[UIDevice currentDevice].batteryLevel];

//         

//     }];

    

    

    

    

    // Do any additional setup after loading the view, typically from a nib.

}