gpt4 book ai didi

Android 处理所有 AlarmManager 限制

转载 作者:行者123 更新时间:2023-11-29 16:30:59 26 4
gpt4 key购买 nike

我在 Play 商店有一个闹钟应用程序,它在大多数设备上运行良好,但不幸的是,有些设备报告闹钟没有在调整后的时间触发,我从研究中得出结论,有些设备限制应用程序在后台运行并杀死警报管理器!

我已经使用以下代码处理了打瞌睡模式:

if (Build.VERSION.SDK_INT >= 23)
{
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeStamp, pendingIntent);
}
else if (Build.VERSION.SDK_INT >= 19)
{
alarmManager.setExact(AlarmManager.RTC_WAKEUP, timeStamp, pendingIntent);
}
else
{
alarmManager.set(AlarmManager.RTC_WAKEUP, timeStamp, pendingIntent);
}

然而,这在某些设备上似乎还不够。

我已经读到前台服务可以防止系统终止应用程序,但是我也不能确保这一点,因为我手头没有发生问题的设备。

我想确保我的闹钟在所有设备上都能正常工作并处理所有情况,那么要确保我的应用程序正常运行并且不会被系统杀死,所有可能的事情是什么?

最佳答案

你应该使用 AlarmManagerCompat.setAlarmClock()设置适用于闹钟应用程序的用户可见闹钟。此 API 使用 setAlarmClock()在 API 21+ 上,根据其文档:

these alarms will be allowed to trigger even if the system is in a low-power idle (a.k.a. doze) mode. The system may also do some prep-work when it sees that such an alarm coming up, to reduce the amount of background work that could happen if this causes the device to fully wake up -- this is to avoid situations such as a large number of devices having an alarm set at the same time in the morning, all waking up at that time and suddenly swamping the network with pending background work. As such, these types of alarms can be extremely expensive on battery use and should only be used for their intended purpose.

关于Android 处理所有 AlarmManager 限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55562609/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com