检测设备是否正在充电

时间:2009-05-08 10:49:34

标签: iphone

我无法使用my favourite tool找到任何明确的内容,但我想我会把它放在这里......

有没有办法使用iPhone SDK,让应用程序检测设备是否处于接收电源状态(充电,停靠等)?

我希望能够在设备接通电源时自动禁用idleTimer(否则它是用户指定的设置)。

10 个答案:

答案 0 :(得分:42)

你最好使用:

[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];

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

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

如果确实希望完成它 - 您将注册接收电池监控通知,因此如果用户断开主电源等,您可以重新启用空闲计时器。

答案 1 :(得分:34)

是的,UIDevice能够告诉你:

[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];

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

有关详细信息,请参阅文档中的UIDevice参考,以及其他值为batteryState的信息。

答案 2 :(得分:6)

Swift 4

:quit!

Swift 3

UIDevice.current.isBatteryMonitoringEnabled = true

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

答案 3 :(得分:5)

斯威夫特3:

doskey ll=dir

答案 4 :(得分:1)

您可以使用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...
  }
} 

答案 5 :(得分:1)

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
        }

    }

答案 6 :(得分:0)

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")

}

答案 7 :(得分:0)

如果您要检查的地方不止一个类似这样的地方。

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
}

答案 8 :(得分:0)

...和@Brad对 Swift 5 的回答:

user joined room {some.id}

答案 9 :(得分:0)

正如许多人所说,您可以使用UIDevice.current.batteryState来检测此状态。

但是您可以做得更多,并使用Notification Center查看状态何时更改。

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

雨燕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
        }
    }