当电池电量低于 20% 时,我需要手机发出蜂鸣声。
我只是不知道如何让我的代码在后台运行。我努力了:
applicationDidEnterBackground'(应用程序:UIApplication)
但它只在应用程序进入后台时检查一次电池;它不更新。
我知道如何获取电池电量。这就是我所拥有的:
var power = Float()
func batteryLevel() {
power = UIDevice.currentDevice().batteryLevel
if power < 0.2 {
println("Beep")
}
}
如何让它在后台定期运行?是否有一堆铁环需要跳过?
最佳答案
您可以使用 NSTimer 每 x 分钟检查一次:
NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector: Selector("checkBattery"), userInfo: nil, repeats: true)
该计时器在 10 分钟后调用 checkBattery
方法,并再等待 10 分钟。
关于ios - 在后台获取电池信息的最优雅的方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32229083/