Visit Sponsor

Written by 5:51 pm Android

Repeat Alarm in Android Using AlarmManager (Android 14 Updated Guide)

Scheduling repeating tasks in Android requires careful handling due to background execution limits introduced in Android 6.0 (Doze Mode) and newer restrictions in Android 12, 13, and 14.

This guide explains how to implement a repeat alarm in Android using AlarmManager, including:

  • Exact alarms support (Android 12+)
  • Runtime notification permission (Android 13+)
  • Doze mode compatibility
  • Best practices and alternatives
  • Full working example

All code and explanations are maintained and updated by JavaTechig.com.

1. When Should You Use AlarmManager?

Use AlarmManager when you need:

  • Exact time-based triggers (e.g., alarm clock)
  • Repeating notifications
  • Calendar reminders
  • Scheduled background tasks

If your task does NOT require exact timing, prefer WorkManager.

2. Android Alarm Types Overview

AlarmManager provides multiple types:

  • RTC_WAKEUP – Triggers at specific wall-clock time and wakes device
  • RTC – Triggers at specific time but does not wake device
  • ELAPSED_REALTIME_WAKEUP – Based on uptime, wakes device
  • ELAPSED_REALTIME – Based on uptime, no wake

For reminder-style alarms, use:

RTC_WAKEUP

3. Android 12+ Exact Alarm Permission

From Android 12 (API 31), exact alarms require special permission.

Add in AndroidManifest.xml:

<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>

For Android 13+ notification permission:

<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

4. Creating a BroadcastReceiver

Create a receiver that will execute when alarm triggers.

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationHelper.showNotification(context);
    }
}

Register it in AndroidManifest.xml:

<receiver android:name=".AlarmReceiver"
    android:exported="false"/>

5. Scheduling a Repeat Alarm

Now create the repeating alarm inside your Activity:

private void scheduleRepeatingAlarm() {

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    Intent intent = new Intent(this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
            this,
            100,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
    );

    long interval = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
    long triggerTime = System.currentTimeMillis() + interval;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        alarmManager.setExactAndAllowWhileIdle(
                AlarmManager.RTC_WAKEUP,
                triggerTime,
                pendingIntent
        );
    } else {
        alarmManager.setRepeating(
                AlarmManager.RTC_WAKEUP,
                triggerTime,
                interval,
                pendingIntent
        );
    }
}

Important:
setRepeating() is not reliable in Doze mode.
Modern Android versions recommend rescheduling inside the receiver.

6. Making It Truly Repeating (Modern Approach)

Because exact alarms fire once, reschedule inside onReceive():

@Override
public void onReceive(Context context, Intent intent) {

    NotificationHelper.showNotification(context);

    AlarmScheduler.scheduleNextAlarm(context);
}

This ensures compatibility with Doze and Android 14 restrictions.

7. Showing Notification (Android 8+ Compatible)

Create a Notification Channel:

public class NotificationHelper {

    public static void showNotification(Context context) {

        String channelId = "alarm_channel";

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    channelId,
                    "Alarm Notifications",
                    NotificationManager.IMPORTANCE_HIGH
            );

            NotificationManager manager =
                    context.getSystemService(NotificationManager.class);

            manager.createNotificationChannel(channel);
        }

        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(context, channelId)
                        .setSmallIcon(R.drawable.ic_notification)
                        .setContentTitle("Reminder")
                        .setContentText("This is your scheduled alarm.")
                        .setPriority(NotificationCompat.PRIORITY_HIGH);

        NotificationManagerCompat.from(context)
                .notify(101, builder.build());
    }
}

8. Cancelling the Alarm

private void cancelAlarm() {

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    Intent intent = new Intent(this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
            this,
            100,
            intent,
            PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
    );

    alarmManager.cancel(pendingIntent);
}

9. AlarmManager vs WorkManager

Use CaseRecommended API
Exact alarm clockAlarmManager
Periodic background syncWorkManager
Push-style tasksFirebase Cloud Messaging

For most repeating background tasks, WorkManager is preferred unless exact timing is required.

10. Common Issues and Fixes

Alarm not triggering?

  • Check battery optimization
  • Verify exact alarm permission
  • Ensure notification permission granted (Android 13+)
  • Use FLAG_IMMUTABLE in PendingIntent

Complete Implementation Flow

  1. Add permissions
  2. Create BroadcastReceiver
  3. Schedule alarm
  4. Show notification
  5. Reschedule inside receiver (modern pattern)

Why This Implementation Is Production-Ready

  • Compatible with Android 6 to Android 14
  • Supports Doze Mode
  • Uses exact alarm handling
  • Follows modern notification system
  • Avoids deprecated APIs

All Android tutorials and updated examples are maintained at JavaTechig.com to ensure developers implement production-ready solutions.

Visited 12 times, 6 visit(s) today
Close