Alarm Manager setRepeating stops working after a while
<p>I know that there are similar questions around but none helped me... I'm trying to set a repeating alarm to fire a broadcast receiver, which will in turn fire an intentservice. I know about doze mode postponing alarms and I don't mind. At first the alarm works fine but after a while it isn't delivered at all. This is how I set the alarm:</p>

<pre><code>    public static void startRepeated(Context context){
        Intent intent=new Intent(context,MyService.class);
        //starting it manually first because API 19+ doesn't deliver exact alarms, but we want a run now
        context.startService(intent);
        intent = new Intent(context,ServiceReceiver.class);
        int minutes=PreferenceManager.getDefaultSharedPreferences(context).getInt("timer",R.integer.timer_def);
        final PendingIntent pendingIntent= PendingIntent.getBroadcast(context.getApplicationContext(),1,intent,PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager=(AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(),minutes*60000,pendingIntent);
    }
</code></pre>

<p>The receiver's (WakefulBroadcastReceiver) onreceive method:</p>

<pre><code>@Override
public void onReceive(Context context, Intent intent) {
    Log.e("SERV-RECEIVER ","triggered");
    startWakefulService(context,new Intent(context,MyService.class));
}
</code></pre>

<p>Note that when this happens, I don't the see SERV-RECEIVER log so not even the receiver is fired. My intent service opens a SQLite db, does some work, closes it and then releases the wakelock. The receiver is registered in android manifest without intents:</p>

<pre><code>&lt;receiver android:name=".ServiceReceiver" android:enabled="true"/&gt;
</code></pre>

<p>I tried to set the device to doze mode and the app to standby manually using adb commands, and after I wake the device up it still works fine. The problem occurs only if I leave it for some time. Using adb shell dumpsys alarm, I noticed that this is always visible (even after the alarm stops </p>

<pre><code>trigerring):
u0a229:com.isoc.android.monitor +2s384ms running, 37 wakeups:
    +2s384ms 37 wakes 37 alarms, last -6m33s804ms:
      *walarm*:com.isoc.android.monitor/.ServiceReceiver
</code></pre>

<p>But this disappears when the problem occurs (from the batch section):</p>

<pre><code>ELAPSED_WAKEUP #0: Alarm{99ac864 tag *walarm*:com.isoc.android.monitor/.ServiceReceiver type 2 when 340221779 com.isoc.android.monitor}
      tag=*walarm*:com.isoc.android.monitor/.ServiceReceiver
      type=2 whenElapsed=-971ms when=-5s971ms
      window=+3m45s0ms repeatInterval=300000 count=0 flags=0x0
      operation=PendingIntent{14dfacd: PendingIntentRecord{82c6f82 com.isoc.android.monitor broadcastIntent}}
</code></pre>

<p>I noticed that on my android API 15 mobile this problem doesn't happen though. Only on my Android marshmallow phone...Any help would be greatly appriciated :)
Thanks</p>