您好,登录后才能下订单哦!
#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.
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。