华为 EMUI 4.0+ 上的电池优化(唤醒锁)华为、电池、EMUI

2023-09-07 10:11:06 作者:跟紧步伐

美好的一天,情况:

我正在开发用作运动追踪器/导航应用程序的 Android 应用程序 - 因此它需要永久连接到 GPS 以及永久唤醒设备.每秒录制一次.

I'm developing Android application that serve as sport tracker/navigation app - so it require permanent connection to GPS and also permanent awake device. Recording is done every second.

目前使用多年的解决方案要归功于 wakelocks 让设备保持唤醒状态.

Current solution working for years is thanks to wakelocks that keep device awake.

Android 6.0+ 中的打盹模式使情况复杂,但事实并非如此.

Doze mode in Android 6.0+ complicate situation, but it is not this case.

在华为设备上可能是不同类型的优化.

On Huawei device is probably different type of optimization.

这是日志的一部分:

10-10 10:33:18.462 1014-384 D/PFW.HwPFWAppWakeLockPolicy: getUidWakeLock uid: 10097 wakelock >= 10 分钟10-10 10:33:18.543 1014-384 D/PFW.HwPFWAppWakeLockPolicy: 强制停止异常唤醒锁应用程序 uid: 1009710-10 10:33:18.558 1014-384 I/ActivityManager: 强制停止 menion.android.locus appid=10097 user=0: from pid 1014

所以大约之后.30 多分钟,系统简单地判断应用程序使用了太多 wakelocks 并使用所有服务、历史记录完全终止它,只需 kill.

So after approx. 30+ minutes, system simply decide that app use too much wakelocks and terminate it completely with all services, history, simply kill.

对此行为的任何经验和任何建议,如何更好地完成这个简单的任务(屏幕关闭时永久记录 GPS 位置)?

Any experience with this behavior and any suggestion, how to this simple task (permanent recording of GPS location when screen is off) better?

正如我在开始时写的那样,在除新华为之外的所有其他设备上,这样的系统可以正常工作多年.

As I wrote at start, on all other devices except new Huawei, such system works correctly for many years.

编辑:一位用户评论后的注释(删除?),华为电池管理器中的白​​名单"应用(标记为受保护的应用")对这个问题没有影响.

EDIT: note after comment of one user (deleted?), "whitelist" app in Huawei battery manager (mark as "protected application") has no effect on this problem.

推荐答案

有两个华为系统应用可能会为了省电而杀死用户应用:

There are two Huawei system apps that may kill user apps to save battery:

SystemManager (com.huawei.systemmanager) 会杀死所有在屏幕关闭后仍在运行的应用,除非它们在受保护的应用"列表中.PowerGenie (com.huawei.powergenie) 会杀死所有长时间持有唤醒锁的应用. SystemManager (com.huawei.systemmanager) kills any apps that are still running after the screen is turned off, unless they're in the "Protected Apps" list. PowerGenie (com.huawei.powergenie) kills any apps that hold wake locks for a long time.

听起来您的应用正在被 PowerGenie 杀死.您可以利用 PowerGenie 的硬编码唤醒锁定标签白名单来避免这种情况.例如,如果您的唤醒锁的标签是LocationManagerService",PowerGenie 将忽略它,因为系统服务持有具有相同标签的唤醒锁,并且 PowerGenie 已将其列入白名单.

It sounds like your app is being killed by PowerGenie. You can avoid this by taking advantage of PowerGenie's hardcoded whitelist of wake lock tags. For example, if your wake lock's tag is "LocationManagerService" it will be ignored by PowerGenie, because a system service holds a wake lock with the same tag and PowerGenie has whitelisted it.