Android Scheduled Task Example using AlarmManager

So here is another useful thing in Android Development. Today we will see an Android Scheduled Task Example using AlarmManager. Scheduling tasks sometimes are essential. For example, your apps need to execute a particular job …

Android Scheduled Task Example

So here is another useful thing in Android Development. Today we will see an Android Scheduled Task Example using AlarmManager. Scheduling tasks sometimes are essential. For example, your apps need to execute a particular job daily at a specific time defined by the user. Then we can do it using the AlarmManager in android. So let’s start.

Android Scheduled Task Example Video

  • If you don’t like following written tutorial, then here we have video explanation for you as well. Instead of reading this post you can watch this video to learn the same.

Android Scheduled Task Example using AlarmManager

Creating a new Project

  • As always we will be creating a new Android Studio Project. So here I am with my project named AlarmManagerExample.

Building UI

  • Inside activity_main.xml we will create the following UI. This is pretty simple we have only a TimePicker and a Button.

Android Scheduled Task Example

  • For the above UI you can use the following XML code.

Creating a Broadcast Receiver for the Alarm

  • We need a Broadcast Receiver to fire the alarm when our app is not running.

Creating BroadcastReceiver

  • So create a java class named MyAlarm.java and write the following code.

Registering BroadcastReceiver

  • We also need to register this BroadcastReceiver in the Manifest file. So here is the AndroidManifest.xml file where you need to register your receiver just before the </application> tag.

Setting the Alarm

  • Now the final step is setting up the Alarm. We will do this inside MainActivity.java.

  • Now thats it you can run the application.

Android Scheduled Task Example

  • Now after setting the alarm even if you close your application. The method will be fired at the specified time. And you can do anything you want as a scheduled task. Right now it will do nothing and will print a message on log as you can see below.

Android Scheduled Task Example

Android Scheduled Task Example Source Code Download

  • If you are having some troubles doing the project. Then here is my source code for you.

[sociallocker id=1372] Android Scheduled Task Example Source Code Download [/sociallocker]

Conclusion

  • So you can use AlarmManager to schedule your tasks. But remember only use AlarmManager when you want to execute your task at a specified time. If you want to schedule tasks other than a time constraint then you should use JobScheduler. We will discuss about it in the upcoming posts.

So thats all for this Android Scheduled Task Example guys. Feel free to leave your comments if having any feedbacks or suggestions. If the post helped you please SHARE and give us a LIKE. Thank You 🙂

 

Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.

Expand Your Knowledge: Next Tutorial Picks

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x