Search code examples
androidalarmmanagerandroid-wifi

My repeat alarm does not work after wifi disconnected


My device does not have a sim card or any network, Except wifi connection.

Now I am setting repeat alarm for daily which working fine when wifi is connected, But it does not trigger on time when wifi is not connected on the Android tablet.

val pendingIntent = PendingIntent.getBroadcast(context, requestCode, archiveIntent, PendingIntent.FLAG_CANCEL_CURRENT)
        val alarms = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
        alarms.cancel(pendingIntent)
        alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP, updateTime.timeInMillis, AlarmManager.INTERVAL_DAY, pendingIntent)

Here are my observations:

  1. Let say I set an alarm at 7:10 PM and when device time change to 7:10 PM it got the call as expected when wifi is connected all the time.

  2. Now the same scenario in which alarm set for 7:10 PM but when device time change to 7:10 PM but at that time wifi is not connected, Now after 7:11 I turn on my wifi on the device, Just after I connected wifi my previous set alarm got called in this case.

Can anyone know what could be reason alarm does not trigger in time when wifi not connected?

NOTE: In my alarm broadcast I am starting WorkManager which had a constraint that it should start only if there is wifi network connected, But I believe that WorkManager should not affect my alarm trigger.


Solution

  • Have a look at this answer

    Its nothing to do with wifi, its the way you set it up, try with setRepeating instead of setInexactRepeating. The difference has explained nicely on above answer.

    Edit

    Eventually got it working after changing the flag to 0 like below

    val pendingIntent = PendingIntent.getBroadcast(context, requestCode, archiveIntent, 0)
    

    Passing 0 will bring any existing alarm back or create a new one.