溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

iOS電池的監(jiān)聽(tīng)

發(fā)布時(shí)間:2020-06-26 00:47:12 來(lái)源:網(wǎng)絡(luò) 閱讀:1067 作者:卓行天下 欄目:移動(dòng)開(kāi)發(fā)


#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.

}



向AI問(wèn)一下細(xì)節(jié)

免責(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)容。

AI