在后台检查电池电量? [英] Check battery level in background?

查看:250
本文介绍了在后台检查电池电量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们的应用程序处于后台时,是否可以在swift中检查电池电量?

Is it possible to check battery level in swift while our application is in background?

目前我正在开发一个iOS应用程序,用户将收到系统警报电池电量达到30%时的通知。

Currently I am working on one iOS application in which user will be alerted with system notification when battery reached to 30%.

我试过,我随机收到通知,有时是4%,有时是10%,比30%还要好。

I tried and I received notification randomly, sometimes 4%, sometimes 10%, mover than ever 30%.

func applicationDidEnterBackground(application: UIApplication) {
    if(UIDevice.currentDevice().batteryLevel <= 0.30 && UIDevice.currentDevice().batteryState != UIDeviceBatteryState.Charging)
    {
        Notification.addNotificationSystem("Mettre le portable à charger", fireDate: 1, iconBadgeNumber: false)
        }
    }
}


推荐答案

您的代码执行电池测试只有一次,当应用程序在后台运行。

如果你想在后台检查电池状态,你的应用程序将是ed能够在那里执行实际代码。这意味着您需要启用其中一种背景模式功能(请注意,如果您声明需要此功能而没有使用任何功能,Apple将拒绝您的应用它)。

Your code executes the battery test only one time, when the app is going in the background.
If you want to check the battery status while in the background, your app will need to be able to execute actual code there. Which means that you will need to enable one of the Background Modes capabilities (note that Apple will reject your app if you declare to need such an ability without any feature using it).

无论如何:如果您的应用被iOS暂停,您将无法查看任何内容。

In any case: If your app gets suspended by iOS, you won't be able to check anything.

这篇关于在后台检查电池电量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆