代码之家  ›  专栏  ›  技术社区  ›  adam

检测充电设备是否正常

  •  25
  • adam  · 技术社区  · 17 年前

    我找不到确切的用途 my favourite tool

    是否有一种方法可以使用iPhone SDK让应用程序检测设备是否处于受电状态(充电、对接等)?

    如果设备正在通电(否则是用户指定的设置),我希望能够自动禁用idleTimer。

    6 回复  |  直到 12 年前
        1
  •  46
  •   Brad    15 年前

    您最好使用:

    [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
    
    if ([[UIDevice currentDevice] batteryState] != UIDeviceBatteryStateUnplugged) {
          [UIApplication sharedApplication].idleTimerDisabled=YES;
        }
    

    这是因为你必须关心你自己 不同的状态-一个是电池处于 ,另一个是 充满电 .

    如果你 真正地

        2
  •  36
  •   Alastair Stuart    15 年前

    是的,UIDevice能够告诉您:

    [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
    
    if ([[UIDevice currentDevice] batteryState] == UIDeviceBatteryStateCharging) {
        NSLog(@"Device is charging.");
    }
    

    UIDevice

        3
  •  11
  •   Oded Regev    8 年前

    斯威夫特4

    UIDevice.current.isBatteryMonitoringEnabled = true
    
    if (UIDevice.current.batteryState != .unplugged) {
        print("Device is charging.")
    }
    

    斯威夫特3

    UIDevice.currentDevice().batteryMonitoringEnabled = true;
    
    if (UIDevice.currentDevice().batteryState != .Unplugged) {
        print("Device is charging.");
    }
    
        4
  •  8
  •   Community Mohan Dere    9 年前

    Swift 3:

    UIDevice.current.isBatteryMonitoringEnabled = true
    let state = UIDevice.current.batteryState
    
    if state == .charging || state == .full {
        print("Device plugged in.")
    }
    
        5
  •  3
  •   OhadM    10 年前

    你可以用 darwin notification center 并使用事件名称 com.apple.springboard.fullycharged .

    通过这种方式,您将收到自定义方法的通知,下面是一个代码剪报:

    // Registering for a specific notification
    NSString *notificationName = @"com.apple.springboard.fullycharged";
    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
                                    NULL,
                                    yourCustomMethod,
                                    (__bridge CFStringRef)notificationName,
                                    NULL, 
                                    CFNotificationSuspensionBehaviorDeliverImmediately);
    

    // The custom method that will receive the notification
    static void yourCustomMethod(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
    {
      NSString *nameOfNotification = (__bridge NSString*)name;
    
      if([nameOfNotification isEqualToString:notificationName])
      {
        // Do whatever you want...
      }
    } 
    
        6
  •  1
  •   King    7 年前

    Swift 4.2

    布尔值

        var batteryState: Bool {
            IDevice.current.isBatteryMonitoringEnabled = true
            let state = UIDevice.current.batteryState
    
            if state == .charging || state == .full {
                print("Device plugged in.")
                return true
    
            } else {
                return false
            }
    
        }
    
        7
  •  0
  •   Elio Lako    7 年前
    func checkForCharging() {
    
    UIDevice.current.isBatteryMonitoringEnabled = true
    
    if UIDevice.current.batteryState != .unplugged {
    
    print("Batery is charging")
    
    } else if UIDevice.current.batteryState == .unplugged {
    
    print("Check the cable")
    
    }
    
        8
  •  0
  •   tBug    6 年前

    如果你想查看不止一个地方,而不是像这样的地方。

    class Device {
    
        static let shared = Device()
    
        func checkIfOnCharcher() -> Bool {
            UIDevice.current.isBatteryMonitoringEnabled = true
            if (UIDevice.current.batteryState != .unplugged) {
                return true
            } else {
                return false
            }
        }
    }
    

    用法:

    if Device.shared.checkIfOnCharcher() == true { 
          //Some Code
    } else { 
       // Some code
    }
    
        9
  •  0
  •   Peter Brockmann    6 年前

    ... 和@Brad的答案 :

        UIDevice.current.isBatteryMonitoringEnabled = true
        if UIDevice.current.batteryState != .unplugged {
            UIApplication.shared.isIdleTimerDisabled = true
        }
    
        10
  •  0
  •   Xaxxus    5 年前

    UIDevice.current.batteryState

    但您可以更进一步,使用通知中心查看状态何时更改。

    下面是一个示例,说明当应用程序插入时,如果您希望屏幕保持清醒:

    斯威夫特5

        class BatteryManager {
            static let shared: BatteryManager = .init()
    
            private init() {
                UIDevice.current.isBatteryMonitoringEnabled = true
        
                NotificationCenter.default.addObserver(
                    self,
                    selector: #selector(batteryStateDidChange),
                    name: UIDevice.batteryStateDidChangeNotification,
                    object: nil
                )
            }
        
            deinit {    
                NotificationCenter.default.removeObserver(
                    self,
                    name: UIDevice.batteryStateDidChangeNotification,
                    object: nil
                )
            }
        
            @objc private func batteryStateDidChange() {
                let state = UIDevice.current.batteryState
        
                UIApplication.shared.isIdleTimerDisabled = state == .full || state == .charging
            }
        }