iOS電池的監聽

來源:互聯網
上載者:User

標籤:interface   import   property   電池   


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

}



本文出自 “ZhuoKing” 部落格,請務必保留此出處http://9951038.blog.51cto.com/9941038/1831784

iOS電池的監聽

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.