


setExactAndAllowWhileIdle() will deliver the alarm at a precise time in the future event the battery is facing issues. Whereas coming to exact alarms, they’re mostly focused on delivering the alarm at the exact time irrespective of the system health. It’s best practice to use inexact alarms if the task to perform is not a high priority. In Android 12 and higher, set(), setInexactRepeating(), or setAndAllowWhileIdle() can deliver the alarm within 60minutes of the trigger time unless there are any battery issues. It’ll deliver the alarm when it thinks it’s most efficient for the battery, mostly it’ll club all the inexact alarms around that time and trigger them once to save battery health. With inexact alarms, the system cannot promise to deliver the alarm at a precise point in time.

Pending intent android studio code#
The above piece of code triggers the alarm after one minute and repeats the alarm every 15 minutes. setRepeating( AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime() + 60 * 1000, 1000 * 60 * 15, alarmIntent ) The only difference is to mention the interval between the alarms and use setRepeating instead of set. Scheduling a repeating alarm is quite similar to a one-time alarm. set( AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 60 * 1000, alarmIntent ) Repeating Alarm Let’s set up an alarm to trigger in one minute. RTC_WAKEUP-Wakes up the device to invoke the pending intent at the given time.RTC-Trigger the pending intent at the given time but does not wake up the device.ELAPSED_REALTIME_WAKEUP-Wakes up the device and triggers the pending intent post the given time has elapsed since device boot.The elapsed time includes any time during which the device was asleep. ELAPSED_REALTIME-Trigger the pending intent based on the time since the device was booted, but doesn't wake up the device.The last two are mostly self-explanatory, whereas coming to an alarm type, AlarmManager offers four types: For this, we need three things, alarm type, trigger time in milliseconds, and the pending intent. FLAG_UPDATE_CURRENT )įinally, it’s time to set an alarm. java) val pendingIntent = PendingIntent.getBroadcast( context, requestCode, alarmIntent, PendingIntent. Have a look: val alarmIntent = Intent(this, AlarmReceiver::class. Now it’s time to create the pending intent to trigger the AlarmReceiver. For now, let’s just create a simple BroadcastReceiver with the name AlarmReceiver as shown below: class AlarmReceiver : BroadcastReceiver() Then we need to create a pending intent that invokes a BroadcastReceiver where we can perform the desired actions like display a notification to the user in our case. To set an alarm first we need to create the AlarmManager class instance as shown below: private var alarmMgr: AlarmManager? = null alarmMgr = context.getSystemService(Context.ALARM_SERVICE) as? AlarmManager? Let’s start with simple one-time(non-repeating) alarms. This comes in handy in various situations like reminding the user to take his medicine or to keep hydrated with an interval of time. You can also schedule repeating alarms with the chosen interval of time. For suppose you can schedule an alarm to remind the user to take his medicine at a particular time in the day. Let’s start by scheduling alarms, AlarmManager class helps us to execute time-based operations outside the scope of the application.
