IOS 特定于设备的开发:检查设备接近度和电池状态

   UIDevice类提供了一些API,使你能够跟踪设备的特征,包括电池的状态和接近度传感器。他们二者都以通知的形式提供更新,可以订阅他们,以便在有重要的更新时通知你的应用程序。

1》启动和禁用接近度传感器

   接近度在此时是一个特定于iPhone的特性。iPod Touch和iPad没有提供接近度传感器。除非具有相对身体部位握持iPhone的某个迫切的理由(或者反之亦然),否则使用接近度传感器获益甚少。

   当启用接近度感应器时,它具有一项主要的任务。他会检测正前方是否有较大的物体。如果是,他将会关闭屏幕,并发送一个普通的通知。把阻挡的物体移开,将会再次打开屏幕。

   下面的例子演示了在iPhone上如何处理接近度传感。他的代码使用UIDevice类切换接近度监测,并且订阅UIDeviceProximityStateDidChangeNotification 以捕获状态改变。两种状态是开和关。当UIDevice proximityState属性返回YES时,就激活可接近度传感器。

2》监测电池状态    

 可以以编程方式跟踪电池和设备状态。这些API使你能够知道电池充电的程度,以及设备是否插入到了充电电源中。电池电量是一个范围在1.0(完全充电)~0.0(完全放电)之间的浮点值。它提供了一个近似放电水平,在执行讲给设备施加罕见重负的操作之钱,可以查询他。

NSLog(@"Battery level:%0.2f%",[UIDevice currentDevice].batteryLevel*100);

充电状态具有四个可能的值:正在充电(既连接到电源),充满,拔掉电源插头和笼统的“未知状态”。可以使用UIDevice batteryState属性取回这些状态

SArray *stateArray = @[@"battery state is unknown",
                            @"battery is not plugged into a charging source",
                            @"battery is charging"
                            @"battery state is full"];
    
    NSLog(@"Battery state:%@",stateArray[[UIDevice currentDevice].batteryState]);

可以通过响应电池状态改变的通知,轻松地监测状态改变。这样,就可以捕获瞬时事件,比如当电池最终充满电时,当用户插入电源充电时,以及当用户断开与电源的连接时。

要开始监测,可以把batteryMonitoringEnabled属性设置为YES,在监测期间,当电池状态或电量改变时,UIDevice类将产生通知。也可以直接检查这些值,而不必等待通知。

- (void)viewDidLoad {
   
 [self updateTitle];
    
    // Add proximity state checker
    [[NSNotificationCenter defaultCenter] addObserverForName:UIDeviceProximityStateDidChangeNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification) {
        NSLog(@"The proximity sensor %@", [UIDevice currentDevice].proximityState ?
              @"will now blank the screen" : @"will now restore the screen");
    }];
    
    // Enable battery monitoring
    [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
    
    // Add observers for battery state and level changes
    [[NSNotificationCenter defaultCenter] addObserverForName:UIDeviceBatteryStateDidChangeNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification) {
        NSLog(@"Battery State Change");
        [self peekAtBatteryState];
    }];
     
    [[NSNotificationCenter defaultCenter] addObserverForName:UIDeviceBatteryLevelDidChangeNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification) {
        NSLog(@"Battery Level Change");
        [self peekAtBatteryState];
    }];
- (void) peekAtBatteryState
{
    NSArray *stateArray = @[@"Battery state is unknown", @"Battery is not plugged into a charging source", @"Battery is charging", @"Battery state is full"];
    
    NSString *status = [NSString stringWithFormat:@"Battery state: %@, Battery level: %0.2f%%",
                        stateArray[[UIDevice currentDevice].batteryState],
                        [UIDevice currentDevice].batteryLevel * 100];
    
    NSLog(@"%@", status);
}
- (void) updateTitle
{
    self.title = [NSString stringWithFormat:@"Proximity %@", [UIDevice currentDevice].proximityMonitoringEnabled ? @"On" : @"Off"];
}

- (void) toggle: (id) sender
{
    // Determine the current proximity monitoring and toggle it
    BOOL isEnabled = [UIDevice currentDevice].proximityMonitoringEnabled;
    [UIDevice currentDevice].proximityMonitoringEnabled = !isEnabled;
    [self updateTitle];
}
原文地址:https://www.cnblogs.com/haibosoft/p/4178023.html