DEV Community

Cover image for How To Schedule Exact Alarms⏱️In Android Using Java☕🧑‍💻
Mohammad Hossein Ilchi
Mohammad Hossein Ilchi

Posted on

How To Schedule Exact Alarms⏱️In Android Using Java☕🧑‍💻

Hey everyone,

In this article, we will explore how to schedule exact alarms in Android using the Alarm Manager. Whether you're developing a To-Do app, an Alarm app, or any application that requires triggering actions or operations at precise times, understanding the Alarm Manager is crucial. This powerful tool allows you to set exact, inexact, and repeating alarms within your Android application. Throughout this article, we'll delve into the usage of the Alarm Manager library specifically for scheduling exact alarms in Java-based Android applications.
Setting exact alarms in Android is very easy. Let's see how it works.

...

Let's Coding

Setting alarms in Android is very simple, and there's no need to use external tools or libraries for this. Implementing it in your Android application is both easy and practical.

Permissions

First, we need to declare some permissions in the Android Manifest file:

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission 
    android:name="android.permission.SCHEDULE_EXACT_ALARM" 
    android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.USE_EXACT_ALARM" />
Enter fullscreen mode Exit fullscreen mode

WAKE_LOCK: This permission allows the app to continue working even when the device is asleep.
SCHEDULE_EXACT_ALARM: This permission is available for Android versions lower than 12 and allows the app to schedule exact alarms.
USE_EXACT_ALARM: This permission is required starting from Android 12 or higher, enabling the app to schedule exact alarms.

Create Broadcast Receiver

Next, we need a broadcast receiver:

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String title = intent.getStringExtra("123231");
        Toast.makeText(context, title, Toast.LENGTH_LONG).show();
    }
}
Enter fullscreen mode Exit fullscreen mode

This broadcast receiver will display a Toast at the appointed time, with a title taken from the intent we send.

Note: Don't forget to declare the broadcast receiver in the manifest file:

<receiver android:name=".AlarmReceiver" />
Enter fullscreen mode Exit fullscreen mode

Declare AlamManager 
In the next part, we'll write the important portion of the code.
First, we need an instance of the "AlarmManager" class:

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Enter fullscreen mode Exit fullscreen mode

Next, we need an Intent:

String title = "Wake up";
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
intent.putExtra("123231", title);
Enter fullscreen mode Exit fullscreen mode

Then, we must create a "PendingIntent" to call the broadcast receiver:

PendingIntent pendingIntent = PendingIntent.getBroadcast(
    context, 
    id, 
    intent, 
    PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
);
Enter fullscreen mode Exit fullscreen mode

To declare a PendingIntent, we need four parameters. The first parameter is the Context, the second is the PendingIntent's ID, the third is the Intent we declared above, and the fourth consists of two flags (you can read more about flags in the link below). Remember, for Android 12 or higher, you must use either PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_MUTABLE to declare the PendingIntent.

Finally, set the time in the AlarmManager:

long triggerAtMillis = System.currentTimeMillis() + 5000;
alarmManager.setExact(AlarmManager.RTC_WAKEUP, triggerAtMillis, pendingIntent);
Enter fullscreen mode Exit fullscreen mode

We can use these three methods to set the time:

  1. .setExact(): This method is used to invoke an alarm at a nearly precise time in the future. However, it does not work when battery-saving mode is enabled.

  2. .setExactAndAllowWhileIdle(): This method works like setExact(), but battery-saving mode has no effect on it.

  3. .setAlarmClock(): This method can set exact alarms like setExact() and works even when battery-saving mode is active. Additionally, the system identifies these alarms as the most critical ones and exits low-power modes if necessary to deliver the alarms.

These methods require three parameters:

  1. The type of alarm. In Android, we can use the following types:
  • ELAPSED_REALTIME: Setting an alarm based on the elapsed time since the device booted, without waking the device from sleep.
  • ELAPSED_REALTIME_WAKEUP: Setting an alarm based on the elapsed time since the device booted, and waking the device from sleep to execute the alarm.
  • RTC: Fires the pending intent at the specified time but does not wake up the device.
  • RTC_WAKEUP: Wakes up the device to fire the pending intent at the specified
  1. The second parameter is the time to call the pending intent (alarm time).
  2. The third parameter is the PendingIntent that we declared above.

Congratulations, you have created an Alarm Manager.
I hope you enjoyed this article and found it helpful.

Top comments (0)