1

I have an app which needs to do task at the exact time provided just like the stock android alarm clock I have used setExactAndAllowWhileIdle() / setExact(). But BroadcastReciever fires after some minutes and sometimes after opening the app, is there any workaround for it?

Alarm Setter code

// set Alarm
Intent intent = new Intent(this.context, AlarmReceiver.class);
intent.putExtra("alarmInfo", extraInfo);
intent.setAction(Long.toString(System.currentTimeMillis()));
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.context, 123456, intent, 0);
AlarmManager alarmManager = (AlarmManager) this.context.getSystemService(Context.ALARM_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {             
   alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, finalTime - 400, pendingIntent);
} else {
   alarmManager.setExact(AlarmManager.RTC_WAKEUP, finalTime - 400, pendingIntent);
}

BroadcastReciever

setNextAlarm();
Log.d("RESULT", "fired");
Intent alarmIntent = new Intent(context, AlarmSoundService.class);
//        Log.d("extra", "alarmInfo: " + intent.getStringExtra("alarmInfo"));
alarmIntent.putExtra("alarmInfo", intent.getStringExtra("alarmInfo"));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    context.startForegroundService(alarmIntent);
} else {
    context.startService(alarmIntent);
}

setNextAlarm() is the previous code I provided which sets the alarm for the future task.

EDIT

Ok so I have solved my problem the first is getting my alarm to fire at the exact moment so setExactAndAllowWhileIdle() / setExact() are both kind of exact so if I need to show user something and have to be accurate I used the setAlarmClock() as the android doc explains

The system may also do some prep-work when it sees that such an alarm coming up, to reduce the amount of background work that could happen if this causes the device to fully wake up

The second problem was that the alarm wasn't firing up unless I open the app this was due to the fact that two alarms were firing between the interval of 5 minutes and since I was using setExactAndAllowWhileIdle() and the android doc explains

Under normal system operation, it will not dispatch these alarms more than about every minute (at which point every such pending alarm is dispatched); when in low-power idle modes this duration may be significantly longer, such as 15 minutes.

And that was it.

  • Well, there are issues for which the receiver fires after somw time, maybe a minute or two. You can try reading answers of [this question](https://stackoverflow.com/q/38094420/8192914) – ganjaam Apr 26 '20 at 14:31
  • However, I didn't encounter problems where the receiver fires after opening the app. It would be helpful to understand the reason if you provide relavent codes. – ganjaam Apr 26 '20 at 14:36
  • @ganjaam I have edited with the code please take a look – Muhammad Uzair Apr 26 '20 at 14:46
  • Remove the call to setExact. It may be overriding the setExactAndAllowWhileIdle, since both go to the same pending intent. You only need one of those two. – Gabe Sechan Apr 26 '20 at 14:51
  • @GabeSechan but the if condition will not ensure just that?? – Muhammad Uzair Apr 27 '20 at 00:41
  • My appologies I missed the if for some reason yesterday. I edited the spacing in your code to make it more clear. – Gabe Sechan Apr 27 '20 at 16:43

0 Answers0