Bozhidar Bozhanov

About Bozhidar Bozhanov

Senior Java developer, one of the top stackoverflow users, fluent with Java and Java technology stacks - Spring, JPA, JavaEE. Founder and creator of Computoser and Welshare. Worked on Ericsson projects, Bulgarian e-government projects and large-scale online recruitment platforms.

Scheduling Repeated Tasks in Android

A somewhat common usecase for android applications is to have them launched when the phone is started, and execute some piece of code periodically.

Sounds straightforward, but there are some pitfalls, so here are a couple of steps to take in order to achieve that. Start with the trivial stuff:

In your manifest you need the following in order to be able to receive the boot event:
 
 
 
 

<uses-permission 
    android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

Then, again in the manifest, you need two receivers:

<receiver android:name=".StartupLauncher">
  <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
    <action android:name="android.intent.action.QUICKBOOT_POWERON" />
    <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>
</receiver>
<receiver android:name=".Notifier">
  <intent-filter>
    <action android:name="com.yourpackage.HOURLY_CHECK" />
  </intent-filter>
</receiver>

What are all these? You will see what HOURLY_CHECK is in a minute. The entries in the .StartupLauncher I gathered from multiple StackOverflow answers, claiming to work on multiple devices. What you are normally required to consume is just BOOT_COMPLETED, but put the rest just in case. Note the enabled attribute of the receiver – they are normally not needed, but make sure the recieve is not disabled by the parent application. Honestly, I wouldn’t advise for putting useless stuff “just in case”, but given the quirks of android devices, I’d keep them.

Now implement the Service that will receive the BOOT_COMPLETED event:

public class StartupLauncher extends BroadcastReceiver {
  @Override
  public void onReceive(final Context ctx, Intent intent) {
    AlarmManager alarmManager = (AlarmManager) ctx.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent("com.yourpackage.HOURLY_CHECK");
    PendingIntent notificationIntent = PendingIntent.getBroadcast(ctx.getApplicationContext(), 1, intent, 0);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 0, TimeUnit.MILLISECONDS.convert(60, TimeUnit.MINUTES), notificationIntent);
  }
}

Why not do it with a ScheduledExecutorService? Because once you schedule a Runnable, your BroadcastReceiver dies, and the executor dies with it, so the runnable is never invoked.

Now you have a scheduled Alarm, where you perform your actual logic:

public class Notifier extends BroadcastReceiver {
  @Override
  public void onReceive(Context ctx, Intent intent) {
    // ...
  }
}

You can of course have a single BroadcastReceiver and distinguish based on the intent, but I think that’s cleaner.

Overall, it’s a process that may be tricky – as it usually happens with Android, unfortunately.

Reference: Scheduling Repeated Tasks in Android from our JCG partner Bozhidar Bozhanov at the Bozho’s tech blog blog.
Related Whitepaper:

Rapid Android Development: Build Rich, Sensor-Based Applications with Processing

Create mobile apps for Android phones and tablets faster and more easily than you ever imagined

Use 'Processing', the free, award-winning, graphics-savvy language and development environment, to work with the touchscreens, hardware sensors, cameras, network transceivers, and other devices and software in the latest Android phones and tablets.

Get it Now!  

Leave a Reply


seven + = 8



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books