AlarmManager - How to repeat an alarm at the top of every hour?

后端 未结 1 1312
既然无缘
既然无缘 2020-12-05 20:36

I want for an event to fire every hour (at 5:00, 6:00, 7:00, etc...). I tried with a persistent background service with a thread but it wasn\'t the right solution because of

相关标签:
1条回答
  • 2020-12-05 21:18

    When you set alarms you have two times: First trigger time, and the next trigger interval.

    You then have to calculate the remaining miliseconds to the next top of the hour, then set one hour for the repeating interval.

    // 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);
    
    0 讨论(0)
提交回复
热议问题