gpt4 book ai didi

android - AlarmManager 使用 API>19 设置精确的重复间隔(无延迟)

转载 作者:太空宇宙 更新时间:2023-11-03 12:47:59 26 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序,它使用我每五分钟用 AlarmManager 调用的服务从惯性传感器收集数据。此服务有一个计时器,仅收集一分钟的数据。所以我想收集一分钟的数据并睡四分钟(循环过程)。问题是带有 setRepeating() 的 AlarmManager 在 API>19 上不准确(延迟一分钟或更长时间)。有很多帖子但没有明确的解决方案......在 MainActivity 中我有:

public void startservice(View view){
AlarmManager scheduler = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), SensorLogger.class);
PendingIntent scheduledIntent = PendingIntent.getService(getApplicationContext(), 0, intent, 0);
scheduler.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 60000 * 5, scheduledIntent);
}

在服务中是这样的:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

timer.scheduleAtFixedRate(new stoptask(), 60000, 60000);

...

return START_NOT_STICKY;
}

private class stoptask extends TimerTask
{
public void run()
{
...

timer.cancel();
stopSelf();
}
}

谢谢!

最佳答案

您不能再准确地使用重复 api。您需要设置一个确切的闹钟,然后在该闹钟内设置一个新闹钟以重复。

我发布了一个负责在 android 上实现多个计时器的库 - 看看 http://gabesechansoftware.com/timers-in-android/这可能会有所帮助。

关于android - AlarmManager 使用 API>19 设置精确的重复间隔(无延迟),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38484432/

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