Search code examples
androidalarmmanagerandroid-pendingintentintentservice

Why service does not work "immediately"?


I have simply functional for start service and repeat he:

mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);

PendingIntent pIntent = PendingIntent.getService(mContext,
                SendStatusService.SEND_STATUS_SERVICE_CODE,
                mIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime() + 2000,
                mIntervalInMs,
                pIntent);

I understand the documentation: alarm manager regardless of device state (sleep or not) start service through 2000 ms and repeat with interval mIntervalInMs.

But service start working after 30-50 sec after running this code. What i make wrong or no understand the documentation?


Solution

  • setRepeating() is "inexact" with a targetSdkVersion of 19 or higher when running on an API Level 19+ Android device. Hence, your results are not surprising. The events will occur somewhere around the desired time (until Android 6.0's Doze mode kicks in), but they will not occur exactly at the desired time.