I used the following code to set 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
 }

Is seems to work fine (it runs almost 20 hours), and in the server I can see that some constant message arrives.

However, there is something about the times: I want the times to be every five minutes, and it seems like in the server I receives the messages in different times.

I add a first sequence of times when the server received messages, when the phone was in sleep mode (in the 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 another 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 to three different anomalies:

  1. Skipping over alarm (for example interval of 10 minutes between two messages in the server) - it seems fine to me, and might be due to connectivity problems.

  2. Pattern of 6 minutes between two messages, and then 4 minutes. You can see this pattern more than one time. I have an assumption, that the OS does some optimization, and if for example it has other alarms every two minutes (for example checking if there are new emails), it runs them together (and the radio is turn on only one time instead of two).

  3. Strange intervals (I can't explain them) - you can see.

So my questions:

  1. How can I enforce the system to run exactly every 5 minutes (or to try harder)?

  2. What are the reasons for the strange timing? I wrote what I think, but these are only thoughts.

Thanks!

有帮助吗?

解决方案

From the API (here):

Note: Beginning with API 19 (KITKAT) alarm delivery is inexact: the OS will shift alarms in order to minimize wakeups and battery use. There are new APIs to support applications which need strict delivery guarantees; see setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent). Applications whose targetSdkVersion is earlier than API 19 will continue to see the previous behavior in which all alarms are delivered exactly when requested.

So, if you are using API 19, then answers to your two questions...

其他提示

How can I enforce the system to run exactly every 5 minutes (or to 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 );
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top