iOS电池的监听
2016-07-29 16:16
483 查看
#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.}
//@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.}
相关文章推荐
- fastjson处理接口时的一个问题
- 解决ie动态修改link样式,import css不刷新的问题
- css import与link的区别
- C#中实现Fluent Interface的三种方法
- C# 6.0的属性(Property)的语法与初始值详解
- Flex include和import ActionScript代码
- C#中Property和Attribute的区别实例详解
- c#中的interface abstract与virtual介绍
- JavaScript中检查对象property的存在性方法介绍
- JavaScript中对象property的删除方法介绍
- JavaScript中遍历对象的property的3种方法介绍
- javascript 对象属性property与元素属性attribute的浏览器支持
- 领悟php接口中interface存在的意义
- phpmyadmin导入(import)文件限制的解决办法
- JavaScript Table排序 2.0 (更新)
- JavaScript对象的property属性详解
- Thinkphp中import的几个用法详细介绍
- C#接口interface用法实例
- PHP abstract与interface之间的区别
- JavaScript中property和attribute的区别详细介绍