2

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:

    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);
    }

The receiver's (WakefulBroadcastReceiver) onreceive method:

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

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:

<receiver android:name=".ServiceReceiver" android:enabled="true"/>

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

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

But this disappears when the problem occurs (from the batch section):

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}}

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

michaelg9
  • 85
  • 2
  • 10

1 Answers1

0

Here is my WakeLocker class

  public class WakeLocker {
private static PowerManager.WakeLock wakeLock;

public static void setWakeLock(Context ctx) {
    PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);

    if (wakeLock != null) wakeLock.release();
    wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "Vocabulary_Alarm");

    wakeLock.acquire();
}

public static void resetWakeLock() {
    if (wakeLock != null) wakeLock.release();
    wakeLock = null;
} }

and in receiver onReceive

  public class AlarmReceiver extends WakefulBroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {

    WakeLocker.setWakeLock(context);


    Intent intentone = new Intent(context.getApplicationContext(), YourActivity.class);
    intentone.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    intentone.putExtra("startTest",true);
    context.startActivity(intentone);

but here if you see I started my required activity and not service.If your problem is not solved even after using WakeLock you too try starting related Activiy.

  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/190737/discussion-on-answer-by-satya-alarm-manager-setrepeating-stops-working-after-a-w). – Cody Gray - on strike Mar 27 '19 at 03:20