Android Alarm Manager for Scheduling Example

Android provides AlarmManager to access its alarm services. In other words alarm manager is used to schedule your application to be run in future. For example if you want to send a notification at some particular time in future then alarm manager can be configured to send a broadcast message when the alarm goes off. One thing to keep in mind is alarms are cleared off if the device is rebooted. Alarm manager can take use of JobIntentService to process long running tasks. You can download the whole project at the end of the post.

Let’s see how we can use Alarm Manager to register an alarm.

  • Open Android Studio and create new project with empty activity
  • Create broadcast receiver to receive alarm broadcasts.
    • Open File -> New -> Other -> BroadcastReceiver
public class AlarmReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        Log.e("TAG", "Alarm received");
        sendNotification(context, intent.getStringExtra("data"));

    public static void sendNotification(Context mcontext, String messageBody) {
        Intent intent = new Intent(mcontext, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(mcontext, 0 /* Request code */, intent,
        NotificationManager notificationManager = (NotificationManager) mcontext.getSystemService(Context.NOTIFICATION_SERVICE);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(mcontext.getString(R.string.default_notification_channel_id), "Rewards Notifications", NotificationManager.IMPORTANCE_DEFAULT);

            // Configure the notification channel.
            notificationChannel.setDescription("Channel description");
            notificationChannel.setVibrationPattern(new long[]{0, 500, 200, 500});
        } notificationBuilder = new NotificationCompat.Builder(mcontext, mcontext.getString(R.string.default_notification_channel_id))

        notificationManager.notify(0 /* ID of notification */,;

The above broadcast receiver sends notification on receiving broadcast from the system. This can be used to create background job as broadcast receiver can be killed by the system once onReceive completes and returns.

Add notification id in strings.xml

<string name="default_notification_channel_id">alarm_channel_id_01</string>
  • Lets see how to schedule an alarm using alarm manager
public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {


    private void scheduleAlarm() {
        Intent alarmIntent = new Intent(this, AlarmReceiver.class);
        alarmIntent.putExtra("data", "Alarm manager example");
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_ONE_SHOT);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        long afterTwoMinutes = SystemClock.elapsedRealtime() + 2 * 60 * 1000;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) 
                        afterTwoMinutes, pendingIntent);
                            afterTwoMinutes, pendingIntent);

After you run this program you will get a notification.

Notification using alarm manager component


Alarm manager provides different types of alarms. The whole list can be checked here.


2.1 MiB

One Response

  1. Numan ARICI December 1, 2020

Leave a Reply