Android AlarmManager turns off by default for 5 minutes when the phone screen is off

So, I have a problem with AlarmManager. I try to run some kind of code every 2 minutes, which works fine while the phone is awake, but not when the device goes into sleep mode - during sleep, the intervals are perfectly located at a distance of 5 minutes.

Since my desired interval is 2 minutes, this is about 250% higher than my target interval, which is unacceptable for my particular application.

I know about the changes in API 19 and have been following the suggestions for redistributing the alarm using setExact () in my BroadcastReceiver. Code below:

Code used to launch BroadcastReceiver:

    Intent intent = new Intent(this, AlarmReceiver.class);
    final PendingIntent pendingIntent = PendingIntent.getBroadcast(this,  0, intent, 0);

    mAlarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    mAlarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 3000, pendingIntent);

And this code is in my BroadcastReceiver, which rewrites the alarm:

@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "AlarmService Triggered.", Toast.LENGTH_SHORT).show();
    Log.d(this.getClass().getSimpleName(), "Service triggered");

    Intent newIntent = new Intent(context, this.getClass());
    mPendingIntent = PendingIntent.getBroadcast(context, 0, newIntent, 0);

    AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 120000, mPendingIntent);
}

- , ? , AlarmManager . , 2 , ?

: Samsung Galaxy S6, OS 5.1.1

+4
2

@Francesc, Samsung. , .

- Samsung, , lol.

+3

ELAPSED_REALTIME_WAKEUP RTC_WAKEUP.

:

        mTargetTime = System.currentTimeMillis() + timeout * 1000L;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, mTargetTime, mPendingIntent);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, mTargetTime, mPendingIntent);
        } else {
            alarmManager.set(AlarmManager.RTC_WAKEUP, mTargetTime, mPendingIntent);
        }
+1

All Articles