AlarmManager setExact with WakefulBroadcastReceiver sometimes not exact

Using Android 19+

setExact in conjuction with WakefulBroadcastReceiver sometimes does not fire on time (can be a few seconds or so late). I mean most it of the time it does. probably 49 times out of 50 its correct.

  • Android - How to programmatically set button color
  • Android Studio 2.2 Preview 1 Rendering Problems
  • How to parse the Json response in android?
  • Package name not shown in Logcat in ICS
  • convert view into bitmap
  • Select + copy text in a TextView?
  • I’m not sure if its just because the system is busy at the time and it can’t handle the workload or what

    Here is how I set the alarm:

      AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
      Intent intent = new Intent(AlarmReceiver.INTENT_FILTER);
      PendingIntent alarmIntent = PendingIntent.getBroadcast(context, MyApplication.ALARM_REQUEST_CODE, intent,  PendingIntent.FLAG_UPDATE_CURRENT);
      alarmMgr.setExact(AlarmManager.RTC_WAKEUP, timeToWakeUp, alarmIntent);

    Here is my receiver code:

    public class AlarmReceiver extends WakefulBroadcastReceiver {
    public static final String INTENT_FILTER = "myfilter";
    public void onReceive(Context context, Intent intent) {
        Intent service = new Intent(context, MyWakefulService.class);
        startWakefulService(context, service);

    And in the WakefulService

    public class MyWakefulService extends IntentService {
    protected void onHandleIntent(Intent intent) {

    Related posts:

    Android: How to create an “Ongoing” notification?
    Display classes containing deprecation Android Studio
    Gradle Sync wont finish after 0.4.6 upgrade
    Can I use assert on Android devices?
    How to animate recyclerview on scroll like Google plus/Google newsstand?
    Eclipse creates fragment layout automatically, how can i disable it
  • Android sdk download/update failing
  • Uri Builder in android - '/' replaced by '%2F' and “:” is replaced by “%3A”
  • Dynamically change height of BottomSheetBehavior
  • Punch a hole in a rectangle overlay with HW acceleration enabled on View
  • Android Studio - Emulator - eglSurfaceAttrib not implemented
  • Is there anyway to write android applications in PHP?
  • 2 Solutions collect form web for “AlarmManager setExact with WakefulBroadcastReceiver sometimes not exact”

    This behaviour is added in API 19:

    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.

    from AlarmManager.

    Important: setExact() still does not have to be exact, as the docs state:

    The alarm will be delivered as nearly as possible to the requested trigger time.

    For Marshmallow era(?), we need some ugly codes like below… 🙁
    And “delayInMillis” param should be more than 15 minutes on the API 23.
    If not, system ignore the minutes less than 15 minutes.

    private void registerExactAlarm(PendingIntent sender, long delayInMillis) {
        final int SDK_INT = Build.VERSION.SDK_INT;
        AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
        long timeInMillis = (System.currentTimeMillis() + delayInMillis) / 1000 * 1000;     //> example
        if (SDK_INT < Build.VERSION_CODES.KITKAT) {
            am.set(AlarmManager.RTC_WAKEUP, timeInMillis, sender);
        else if (Build.VERSION_CODES.KITKAT <= SDK_INT  && SDK_INT < Build.VERSION_CODES.M) {
            am.setExact(AlarmManager.RTC_WAKEUP, timeInMillis, sender);
        else if (SDK_INT >= Build.VERSION_CODES.M) {
            am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeInMillis, sender);
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.