Create Background Tasks with Alarm Manager in Android
Last updated Dec 23, 2021In this android example tutorial, we will learn how to create background task with alarm manager in android studio using kotlin language.
AlarmManager: AlarmManager acts as a link between the application and the Android system alarm service. It can send a broadcast to your app (which the user can entirely discontinue) at a certain time, and your app can then conduct any task as a result. The system can postpone any alarm in order to improve device performance.
Task scheduling is occasionally necessary. For example, your apps may be required to perform a specific task on a daily basis at a time specified by the user. Then we may use Android's AlarmManager to do it. So let's get started.
Implementation:
Step 1. Create a new Project in android studio.
Go to File > New > New Project > Google Maps Activity > Next > Enter Name > Select Language Kotlin > Finish. |
After creating the new project, Android Studio starts Gradle and builds your project, which may take a few seconds.
Step2. The following UI will be created within activity_main.xml file. We only have a TimePicker and a Button in this case.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<Button </LinearLayout>
|
TimePicker: The Android TimePicker is a user interface control that allows you to select the time in 24-hour format or AM/PM mode. It is utilised in our application to verify that users select the correct time for the day.
![]() |
Above xml code will give you output like this.
Step 3. Create a new kotlin file in project name AlarmReceiver.
app > java > com.example.alarmmanager > right-click > new > Kotlin Class/File > Enter Name (AlarmReceiver) > Enter. |
Step 4. Open AlarmReceiver.kt file and add the following code
class AlarmReciever : BroadcastReceiver() { } |
Broadcast Receivers merely reply to broadcast messages sent by other apps or the system. These signals are sometimes referred to as happenings or intentions. A broadcast receiver is implemented as a subclass of BroadcastReceiver class and overriding the onReceive() method where each message is received as a Intent object parameter.
Step 5. Registering BroadcastReceiver
This BroadcastReceiver must also be registered in the Manifest file. So, in the AndroidManifest.xml file, immediately before the tag.
<receiver android:name=".AlarmReciever" android:enabled="true" android:exported="true" /> |
Step 6. Go to MainActivity.kt file and add the following code
Instantiate the views by its unique id of TimePicker and Button.
val timePicker = findViewById(R.id.timePicker) |
Attach onClick event on button
setAlarm.setOnClickListener(object : View.OnClickListener { |
Build.VERSION>SDK_INT >= 23 (Check if we're running on Android 6.0 or higher)
Create a function setAlarm for setting up the alarm
private fun setAlarm(time: Long) { //creating a new intent specifying the broadcast receiver //creating a pending intent using the intent //setting the repeating alarm that will be fired every day |
Step 7. Now run the app in your emulator or real device, you will get the following output:
OUTPUT:
![]() |
![]() |
Even if you close your application after setting the alarm. The method will be called at the time indicated. And as a scheduled task, you can perform whatever you want. Right now, it will do nothing and will output a message to the log, as shown below.
![]() |
Complete Source Code of Alarm Manager Example:
activity_main.xml file
<?xml version="1.0" encoding="utf-8"?>
<Button </LinearLayout> |
MainActivity.kt file
import android.app.AlarmManager import android.widget.TimePicker
//Instantiate the views TimePicker and Button
//attaching clicklistener on setAlarm button private fun setAlarm(time: Long) { //creating a new intent specifying the broadcast receiver //creating a pending intent using the intent //setting the repeating alarm that will be fired every day
|
AlarmReceiver.kt file
import android.content.BroadcastReceiver class AlarmReciever : BroadcastReceiver() { } |
AndroidManifest.xml file
<?xml version="1.0" encoding="utf-8"?> <category android:name="android.intent.category.LAUNCHER" /> </manifest>
|
Conclusion: In this article we have covered how to create Background task with alarm manager in Android apps using Kotlin language.
Article Contributed By :
|
|
|
|
1880 Views |