iOS电池的监听

简介:

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

}











本文转自 卓行天下  51CTO博客,原文链接:http://blog.51cto.com/9951038/1831784,如需转载请自行联系原作者
目录
相关文章
|
移动开发 前端开发 JavaScript
React Native | 原生IOS模块与JS通信,监听App被Kill
React Native | 原生IOS模块与JS通信,监听App被Kill
498 0
|
JavaScript Android开发 iOS开发
兼容安卓和ios的手机端浏览器返回和物理返回的监听处理操作实战(推荐)
兼容安卓和ios的手机端浏览器返回和物理返回的监听处理操作实战(推荐)
311 0
兼容安卓和ios的手机端浏览器返回和物理返回的监听处理操作实战(推荐)
|
iOS开发
iOS开发-监听手机左上角系统自带的返回按钮
iOS开发-监听手机左上角系统自带的返回按钮
202 0
|
前端开发 开发工具 Android开发
iOS监听物理截图自动生成截图并跳转到反馈页面进行显示(截图内容包括系统的弹框视图UIAlertView/Controller)
iOS监听物理截图自动生成截图并跳转到反馈页面进行显示(截图内容包括系统的弹框视图UIAlertView/Controller)
352 0
iOS监听物理截图自动生成截图并跳转到反馈页面进行显示(截图内容包括系统的弹框视图UIAlertView/Controller)
横屏时,ios7上有电池状态栏,在iOS8就没有电池状态栏了
上次开发过程遇到有个页面横屏显示,当设置横屏展示时,就没有电池状态栏了。查了一些资料才发现,原来 横屏时,ios7上有电池状态栏,在iOS8就没有电池状态栏了,是因为iOS8默认横屏时将电池状态栏隐藏了,这是iOS8的新特性。
|
API
iOS使用锁屏监听的私有API被拒 com.apple.springboard.lockcomplete
最近公司有个项目需要对锁屏进行监控以便进行一些操作,然后在更新新版本的时候,审核竟然被拒绝了。原因竟然是调用了 Apple 不允许使用的 锁屏API ,如下方法一;后来改成方法二,终于审核通过了。
4015 0