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 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.

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 🙂

 

15 thoughts on “Android Scheduled Task Example using AlarmManager”

  1. Belal bhai …not working …my device is Lenovo A600 android 5.0 I was waiting for so long

    I have an issue with notification ..

    they occur only if app is in background or in foreground i.e service is stopped when i swipe right app from the list of the current running apps .
    Please Help !!!!

    Reply
  2. perhaps it is because you didn’t write in myalarm.java

    public void onReceive(Context context, Intent intent) {

    MediaPlayer mediaPlayer = MediaPlayer.create(context, Settings.System.DEFAULT_RINGTONE_URI);
    mediaPlayer.start();
    Log.d(“MyAlarmBelal”, “Alarm just fired”);
    }

    Reply
  3. 03-14 10:22:32.180 4257-4274/chatapp.com.alarmdemo2 E/Surface: getSlotFromBufferLocked: unknown buffer: 0x7f80fe2280
    whenever in click on set alarm button it gives this in logcat..
    please help

    Reply
  4. Hi Belal, I am using your code, but the scheduled alarms are not firing if the application is killed or mobile rebooted. I am using asus zenfone m1 pro. and letv mobiles. Both are running on oreo and marshmallow respectively.

    Reply
  5. Hello,
    I think BroadcastReceiver is not working properly after nougat OS, i.e alarm ,notifications.
    please help me. I am new in android

    Reply
  6. Hi Developer, your code is not working. The scheduled alarms are not firing if the application is killed or mobile rebooted.Please give another solution..I am using huawei honor 6 marmallow.

    Reply
  7. Hi Bilal!

    I am working with alarm manager. I have created alarm for 09:00 am. I want to run my broadcast receiver at that time but I face issue. When ever I run my app my broadcast receiver fired and if my app is running broadcast receiver fired again and again until the app is closed. Kindly help me to resolve this issue.

    Reply

Leave a Comment