资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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电池的监听
文章路径:http://cdkjz.cn/article/ppghej.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220