iOS电池的监听

#import"ViewController.h"

创新互联公司主要从事成都做网站、网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务盘龙,十余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220

#import

@interfaceViewController ()

//@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 {

    [superviewDidLoad];

   //打开电池的监听

    [UIDevicecurrentDevice].batteryMonitoringEnabled =YES;

   //获取电池的状态

   UIDeviceBatteryState BatteryState = [UIDevicecurrentDevice].batteryState;

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

   CGFloat batterylevel = [UIDevicecurrentDevice].batteryLevel;

   //battery的状态分为:

   /*

     UIDeviceBatteryStateUnknown,    未知

     UIDeviceBatteryStateUnplugged,   //未充电

     UIDeviceBatteryStateCharging,     //正在充电

     UIDeviceBatteryStateFull,             //满电

     

     */

   if (BatteryState ==UIDeviceBatteryStateUnknown) {

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

       NSLog(@"unknow");

    }else{

       NSLog(@"know");

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

       self.dlLabel.text = [NSStringstringWithFormat:@"%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.

}


网页标题:iOS电池的监听
网页URL:http://scjbc.cn/article/ppghej.html

其他资讯