android - AlarmManager - 如何在每个小时的顶部重复一个警报?

标签 android repeat alarmmanager

我想要一个事件每小时触发一次(在 5:00、6:00、7:00 等...)。 我尝试使用带有线程的持久后台服务,但它不是正确的解决方案,因为:

  • 电池消耗
  • 服务终止,由于 android 内存管理

所以我正在尝试使用 AlarmManager。如果我将警报设置为在 X 秒内触发(使用“设置”方法),它就会起作用。 但是我怎样才能在每个小时的顶部重复一个事件(使用“setRepeating”方法),直到警报被取消?

谢谢!

最佳答案

设置闹钟时有两次:第一次触发时间和下一次触发间隔。

然后您必须计算到下一个小时的剩余毫秒数,然后将重复间隔设置为一小时。

// We want the alarm to go off 30 seconds from now.
long firstTime = SystemClock.elapsedRealtime();
firstTime += remainingMilisecondsToTopHour;
long a=c.getTimeInMillis();

// Schedule the alarm!
AlarmManager am = (AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME,
c.getTimeInMillis(), 1*60*60*1000, sender);

关于android - AlarmManager - 如何在每个小时的顶部重复一个警报?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3127351/

相关文章:

java - AsyncTask在doInBackground中止

android - 在 Android 上通过 NFC 读取 Visa payWave 信用卡详细信息

iphone - 如何重复UILocalNotification声音

Android AlarmManager - RTC_WAKEUP 与 ELAPSED_REALTIME_WAKEUP

java - 安卓报警管理器 : how to find out the time remaining to trigger the PendingIntent

android 使用 ContactsContract 读取联系人详细信息

android - 接收器未注册

javascript - JavaScript 中重复重复函数的最佳方法?

php - 如何避免在 PHP 中重复 MySQL 查询代码的类似部分?

Android 通知只显示一次