Over a million developers have joined DZone.

Scheduling Repeated Tasks in Android

DZone's Guide to

Scheduling Repeated Tasks in Android

· Java Zone
Free Resource

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

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:

android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

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

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

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

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.


Published at DZone with permission of Bozhidar Bozhanov, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}