您好,登錄后才能下訂單哦!
#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之間的浮點(diǎn)值。
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//打開(kāi)電池的監(jiān)聽(tīng)
[UIDevice currentDevice].batteryMonitoringEnabled = YES;
//獲取電池的狀態(tài)
UIDeviceBatteryState BatteryState = [UIDevice currentDevice].batteryState;
//獲取剩余電量 范圍在0.000000 至 1.000000之間
CGFloat batterylevel = [UIDevice currentDevice].batteryLevel;
//battery的狀態(tài)分為:
/*
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.
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。