Home > Researches > Android – Scheduling an application to start later.

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 {

  @Override
  public void onReceive(Context context, Intent intent) {

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

     // Start the MainActivity
     Intent i = new Intent(context, MainActivity.class);
     i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     context.startActivity(i);
  }
}

Register the OnAlarmReceive in the AndroidManifest file:


<application
   android:icon="@drawable/ic_launcher"
   android:label="@string/app_name" >

   // ......

   <receiver
      android:name=".OnAlarmReceive" />

</application>

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,
     PendingIntent.FLAG_UPDATE_CURRENT);

  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
  MainActivity.this.finish();
}

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.

About these ads
  1. July 3, 2012 at 5:46 pm

    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. Question...
    September 25, 2012 at 2:11 am

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

    • October 6, 2012 at 12:28 pm

      You put it in the activity where you want to set up the alarm. So yes, you can put it in MainActivity.

  3. ask
    November 24, 2012 at 8:05 pm

    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

    • November 24, 2012 at 9:18 pm

      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. ask
    November 27, 2012 at 1:37 am

    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.

  5. May 11, 2013 at 3:58 am

    Very nice demo. I tried a few other demos before this one and this is the only one that worked.

  1. February 2, 2013 at 8:00 am

Leave a Reply

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

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 517 other followers

%d bloggers like this: