I used the following code to set a repeating alarm (every 5 minutes).
public void SetAlarm(Context context) { AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); Intent i = new Intent(context, Alarm.class); PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0); am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 5, pi);
It seems to work fine (it works for almost 20 hours), and on the server I see that some kind of constant message is coming.
However, there is something about time: I want the time to be every five minutes, and it looks like I am receiving messages on the server at different times.
I add the first sequence of times when the server received messages when the phone was in sleep mode (at night):
05:13:51, 05:18:54, 05:24:54, 05:28:54, 05:33:51, 05:38:54, 05:52:45, 05:54:54, 05:58:52, 06:04:54, 06:08:54, 06:16:19, 06:18:54, 06:24:54, 06:28:54, 06:34:54, 06:48:42, 06:48:44, 06:58:54,
And one more sequence when I used the phone from time to time:
11:08:46, 11:13:45, 11:18:48, 11:23:52, 11:33:54, 11:38:47, 11:48:47, 11:58:47, 12:03:52, 12:08:45, 12:14:49, 12:18:43, 12:25:37, 12:28:41, 12:34:56, 12:38:47, 12:43:48, 12:48:56, 12:54:07, 12:58:48, 13:03:43, 13:08:56, 13:14:11, 13:18:55, 13:25:02, 13:28:45, 13:33:43, 13:44:57, 13:48:58, 13:54:57, 13:58:52, 14:03:58
I notice three different anomalies:
Alarm skipping (for example, 10 minutes interval between two messages on the server) - it seems to me that this is good for me and may be due to connection problems.
Figure 6 minutes between two messages, and then 4 minutes. You can see this template more than once. I have an assumption that the OS does some optimization, and if, for example, it has other alarms every two minutes (for example, checking for new email messages), it starts them together (and the radio turns on only once instead of two) .
Strange intervals (I cannot explain them) - you can see.
So my questions are:
How can I make the system work exactly every 5 minutes (or try harder)?
What are the reasons for the strange time? I wrote what I think, but these are only thoughts.
Thank!