gpt4 book ai didi

android - AlarmManager 在 5 分钟后触发,但未在所需时间触发

转载 作者:太空狗 更新时间:2023-10-29 14:02:03 25 4
gpt4 key购买 nike

我正在使用警报管理器每 40 秒触发一次 Intent 服务。下面的代码是我触发警报和启动服务的方式。我知道警报并不总是在 40 秒后准确触发,并且可能会有所不同。

但我遇到了一个情况,其中警报变化很大,并且每 5 分钟就会不断触发一次。

启动闹钟:

Intent alarmIntent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
long interval = 40000;
manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);

报警接收器:

@Override
public void onReceive(Context arg0, Intent arg1)
{
globalVariables.DebugLogging("IsSignedIn: "+GlobalVariables.IsSignedIn+" -DownloadServiceCanStart:- "+GlobalVariables.DownloadServiceCanStart);
if(GlobalVariables.IsSignedIn)
{
if (GlobalVariables.DownloadServiceCanStart)
{
GlobalVariables.DownloadServiceCanStart=false;
Intent intent = new Intent(arg0, DownloadService.class);
context.startService(intent);
}
}
}

最佳答案

AlarmManager不是很精确。

正如官方文档所述:

Note: Beginning with API 19 (KITKAT) alarm delivery is inexact: the OS will shift alarms in order to minimize wakeups and battery use. There are new APIs to support applications which need strict delivery guarantees; see setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent). Applications whose targetSdkVersion is earlier than API 19 will continue to see the previous behavior in which all alarms are delivered exactly when requested.

还有 60 秒的最小间隔。因此,您的 40 秒间隔将扩展到 60 秒。

如果你想每 40 秒运行一次,你可以调查这个答案: https://stackoverflow.com/a/9539579/503508

关于android - AlarmManager 在 5 分钟后触发,但未在所需时间触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34854077/

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