ios - 应用程序处于后台模式时的电池电量更新值 swift
问题描述
当应用程序在后台时,我也需要获取 batteryLevelDidChangeNotification 值。我已经在 applicationDidEnterBackground 中编写了代码,但当电池电量变化时我也没有受到打击。我也在后台模式下检查了后台获取功能。 这是我的代码
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UIDevice.current.isBatteryMonitoringEnabled = true
NotificationCenter.default.addObserver(self, selector: #selector(batteryLevelDidChange), name: UIDevice.batteryLevelDidChangeNotification, object: nil)
return true
}
@objc func batteryLevelDidChange(_ notification: Notification) {
print(batteryLevel)
}
func applicationDidEnterBackground(_ application: UIApplication) {
UIDevice.current.isBatteryMonitoringEnabled = true
NotificationCenter.default.addObserver(self, selector: #selector(batteryLevelDidChange), name: UIDevice.batteryLevelDidChangeNotification, object: nil)
}
解决方案
推荐阅读
- c# - Websocket服务器没有从客户端获取所有消息
- java - 如何为我打印的每个“行”创建一个列表或数组列表
- java - Java parsefloat 在每个 CPU 上的结果相同
- javascript - 在 React Native 中垂直对齐文本
- r - 在 Linux EC2 上安装钠包时出错 - 无法加载共享对象
- java - 如何在本地目录中将 HttpResponse 保存为 PDF 文件
- iot - 无法在物联网平台中建立云基础应用程序和云基础服务之间的连接
- java - 如何从 Java 的 for 循环中知道哪个元素异常?
- wordpress - 构建自定义 Elementor Widget-Wordpress
- javascript - @azure/storage-blob 中的 TokenCredential 中的令牌是什么,如何获取?