Alarms
The AlarmManager is a system service that allows you to schedule and perform actions at specified times or intervals, even if your application is not currently running. It provides a way to schedule your application to execute code at a specific time or repeatedly after a certain interval.
The AlarmManager class provides several methods to set and manage alarms, including:
set()
- Sets a one-time alarm that will trigger the specified PendingIntent at the specified time.setRepeating()
- Sets a repeating alarm that will trigger the specified PendingIntent at a specified interval, starting from the specified time.setExact()
- Sets a one-time alarm that will trigger the specified PendingIntent exactly at the specified time, without any inexactness.setExactAndAllowWhileIdle()
- Similar tosetExact()
, but allows the alarm to trigger even if the device is in low-power idle mode.setInexactRepeating()
- Sets a repeating alarm that will trigger the specified PendingIntent at a specified interval, but with inexact timing to optimize battery usage.
These are some of the commonly used methods of AlarmManager. Each method requires a PendingIntent, which encapsulates the action to be performed when the alarm triggers, such as starting a service, sending a broadcast, or launching an activity.
The AlarmManager supports different types of alarms, including:
RTC_WAKEUP - This alarm type uses the real-time clock (RTC) and wakes up the device to trigger the alarm. It ignores any sleep or low-power states of the device. Useful for time-sensitive alarms that need to wake up the device from sleep.
RTC - This alarm type uses the real-time clock (RTC) but does not wake up the device. The alarm will be delivered to the application when the device is next awake. Useful for alarms that don't require immediate execution.
ELAPSED_REALTIME_WAKEUP - This alarm type uses the elapsed real-time clock and wakes up the device to trigger the alarm. It does not use the wall clock time and ignores sleep or low-power states. Useful for time-sensitive alarms that need to wake up the device from sleep.
ELAPSED_REALTIME - This alarm type uses the elapsed real-time clock and does not wake up the device. The alarm will be delivered to the application when the device is next awake. Useful for alarms that don't require immediate execution.
When setting an alarm, you need to specify the alarm type and provide the time or interval at which the alarm should trigger. The AlarmManager takes care of managing the alarms even if the device is rebooted, ensuring that the scheduled actions are still triggered as intended.
Example: Here's a simple example of how you can implement an alarm in Android Studio using Java:
First, you'll need to create an XML layout file for the alarm activity. Create a new file called activity_alarm.xml
in the res/layout
directory and add the following code:
Next, create a new Java class called AlarmActivity
. Replace the existing code in the class with the following code:
In this code, we set the alarm to trigger at 8:00 AM. You can modify the hour and minute values in the setAlarm()
method as per your requirement.
Next, create another Java class called AlarmReceiver
. This class will handle the alarm event when it is triggered. Replace the existing code in the class with the following code:
Finally, update the AndroidManifest.xml file to include the necessary permissions and declarations. Add the following lines inside the <manifest>
tag:
That's it! You've implemented a basic alarm functionality in Android Studio using Java. When you run the app and click the "Set Alarm" button, it will schedule an alarm for the specified time. When the alarm triggers, a toast message saying "Alarm triggered!" will be displayed.
Last updated
Was this helpful?