Android – Scheduling an application to start later.

Recently I have been working on a simple application that should have the ability to start itself after a period of time, after the application is closed. Hopefully, it turned out that this is quite simple to implement, and to achieve this, the AlarmManager in conjuction with a BroadcastReceiver can be used.

The basic idea is as follows: the AlarmManger registers an intent, when the alarm goes off, the intent that had been registered automatically will start the target application if it is not already running. The BroadcastReceiver will be listening for that intent, and when the intent is received, it will start the desired activity from the application.

The broadcast receiver implementation looks like this:

public class OnAlarmReceive extends BroadcastReceiver {

  public void onReceive(Context context, Intent intent) {

     Log.d(Globals.TAG, "BroadcastReceiver, in onReceive:");

     // Start the MainActivity
     Intent i = new Intent(context, MainActivity.class);

Register the OnAlarmReceive in the AndroidManifest file:

   android:label="@string/app_name" >

   // ......

      android:name=".OnAlarmReceive" />


And finally, here’s how to setup the alarm:

* Sets up the alarm
* @param seconds
*            - after how many seconds from now the alarm should go off
private void setupAlarm(int seconds) {
  AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
  Intent intent = new Intent(getBaseContext(), OnAlarmReceive.class);
  PendingIntent pendingIntent = PendingIntent.getBroadcast(
     MainActivity.this, 0, intent,

  Log.d(Globals.TAG, "Setup the alarm");

  // Getting current time and add the seconds in it
  Calendar cal = Calendar.getInstance();
  cal.add(Calendar.SECOND, seconds);

  alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

  // Finish the currently running activity

The last line from the code – finishing the current activity – is optional of course, but in case you need to finish the activity, here’s how to do.

For a live demo of the code, download the Amazing Cracked Screen application and see how it works. There you have the possibility to set up the delay in seconds when the application should start later and show the “broken” screen.


10 thoughts on “Android – Scheduling an application to start later.

  1. Why thank you so much for ‘liking’ my blog post! Wasn’t sure if anyone would find me since I’ve just begun — I’m glad you did! Really cool stuff you have here. I’m enjoying looking around. Btw I have published a new post…would love to here from you…

  2. I dont understand where do i put the “private void setupAlarm” part. Do i put it in the MainActivity class or what?

  3. I dont understand what ” Globals ” is supposed to be , iam just starting with the android and this the only error that i found in the code, so please can you help ? thank you

    1. The “Globals” part it is not really necessary, I just have a class named Globals where I have declared some constants so I can access them globally in the application. In that particular case, I’m refering to a constant named “TAG” which is just a String so I can see some messages in the LogCat to make sure the broadcast receiver is triggered.

      You can delete Global.TAG, and instead of it write any string value you want, or comment that line..

  4. Sorry but iam so confused, where can i put “The broadcast receiver implementation” part , do i have to make a new Activity named broadcast receiver or what ? and if i made a new activity do i put the ” setup the alarm” part in it or in the MainActivity part ?
    and how can i set the time required ? for example if i want to set it on 12:00 PM how can i write it ?
    i just want to make a program that shows a photo on the screen at a specific time.
    Sorry for asking too many questions but i really need help.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s