In this article, we will see how to use an AlarmManager class in Kotlin to provide a convenient way to schedule and manage alarms, display notifications, start a service e.t.c

First, we need to obtain an instance of the AlarmManager

val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager

We also need to create an Intent and specify the action to be performed when the alarm triggers.

val intent = Intent(this, MyAlarmReceiver::class.java)

Now, we have to create a pending intent using the Intent which takes the desired action when the alarm triggers.

val pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0)

Set up the alarm using AlarmManager’s set() method. Specify the trigger time, type of alarm, and the PendingIntent.

In the following example, we set the alarm to trigger 10 seconds from the current time using System.currentTimeMillis(). AlarmManager.RTC_WAKEUP is used to wake up the device when the alarm is triggered.

val triggerTime = System.currentTimeMillis() + 10000 // 10 seconds from now
alarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent)

Now, we need to create a broadcast receiver to receive the alarm and define the actions to be performed.

class MyAlarmReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        // Actions to perform when the alarm triggers
    }
}

Example:

Let us see an example to display the notification 30 seconds after a button click by using the alarm manager.

import android.app.AlarmManager
import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    private lateinit var alarmManager: AlarmManager

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager

        val button = findViewById<Button>(R.id.button)
        button.setOnClickListener {
            scheduleNotification()
        }
    }

    private fun scheduleNotification() {
        val intent = Intent(this, MyAlarmReceiver::class.java)
        val pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0)

        val triggerTime = System.currentTimeMillis() + 30000 // 30 seconds from now
        alarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent)
    }

    class MyAlarmReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            // Display the notification here
        }
    }
}

In the above example, we have a button with the ID button in our layout XML file. When the button is clicked, the scheduleNotification() function will be called which sets up the alarm to trigger 30 seconds from the current time.

When the alarm triggers, the onReceive() function will be called and we can define the actions here such as displaying a notification.

Categorized in:

Tagged in:

,