Android Alarm Manager starts in weird times

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); // Millisec * Second * Minute } 

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!

+9
performance android alarmmanager alarm
Dec 03 '13 at 13:14
source share
2 answers

From the API ( here ):

Note. Starting with API 19 message delivery (KITKAT) is inaccurate: the OS will switch alarms to minimize wake-ups and battery usage. There are new APIs to support applications requiring strict warranty delivery; see setWindow (int, long, long, PendingIntent) and setExact (int, long, PendingIntent). Applications, targetSdkVersion earlier than API 19, will continue to see the previous behavior in which all alarms are transmitted exactly when requested.

So, if you use API 19, then the answers to two questions ...

+7
Dec 03 '13 at 13:30
source share

How can I make the system work exactly every 5 minutes (or try harder)?

Try this code, it works 100%

 Calendar calendar = new GregorianCalendar(); calendar.setTimeZone(TimeZone.getDefault()); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.set( Calendar.MINUTE, 00 ); calendar.set( Calendar.SECOND, 00 ); Intent intent = new Intent(context, Alarm.class); PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0 ); hourlyAlarm = (AlarmManager) context.getSystemService( Context.ALARM_SERVICE ); hourlyAlarm.setRepeating( AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),1000 * 60 * 5, pi ); 
-one
Dec 03 '13 at 13:23
source share



All Articles