Scheduling code execution on Android using AlarmManager

One of the advantages android gives developers over iOS is the ability to schedule code execution. This means your app can do something in the background at a specific time. In this example Im going to create a sample app that puts your phone on silent at midnight, and unsilences it at 6AM. This can be very useful for developers who need their app to do something more complex than popping up a notification at a specific time. Lets get into it!

 

The first thing you will need to do in your app is create two classes that extend the BroadcastReceiver class, and give them some code you want to schedule in the onReceive method:

 


public class SilenceBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
AudioManager audio = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);

}

}

 


public class UnsilenceBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
AudioManager audio = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
}

 

Next we want to make an entry in our manifest so that we can point to these Broadcast Receivers in our scheduling code. Add the nodes for these receivers inside of the Application tag in the manifest file:

 


<receiver android:name="Silence" >
<intent-filter>
<action android:name="net.accella.sheduleexample.SilenceBroadcastReceiver" >
</action>
</intent-filter>
</receiver>

<receiver android:name="Unsilence" >
<intent-filter>
<action android:name="net.accella.sheduleexample.UnsilenceBroadcastReceiver" >
</action>
</intent-filter>
</receiver>

 

Next we’re going to write the code that sets the schedule. Here you’ll notice when we touch the “Schedule” button it schedules two repeating events to run at midnight and at 6 AM:

schedule.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
//create new calendar instance
Calendar midnightCalendar = Calendar.getInstance();
//set the time to midnight tonight
midnightCalendar.set(Calendar.HOUR_OF_DAY, 0);
midnightCalendar.set(Calendar.MINUTE, 0);
midnightCalendar.set(Calendar.SECOND, 0);
AlarmManager am = (AlarmManager) LayoutActivity.this.getSystemService(ALARM_SERVICE);
//create a pending intent to be called at midnight
PendingIntent midnightPI = PendingIntent.getService(LayoutActivity.this, 0, new Intent("net.accella.sheduleexample.SilenceBroadcastReceiver"), PendingIntent.FLAG_UPDATE_CURRENT);
//schedule time for pending intent, and set the interval to day so that this event will repeat at the selected time every day
am.setRepeating(AlarmManager.RTC_WAKEUP, midnightCalendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, midnightPI);

Calendar sixCalendar = Calendar.getInstance();
//set the time to 6AM
sixCalendar.set(Calendar.HOUR_OF_DAY, 6);
sixCalendar.set(Calendar.MINUTE, 0);
sixCalendar.set(Calendar.SECOND, 0);

//create a pending intent to be called at 6 AM
PendingIntent sixPI = PendingIntent.getService(LayoutActivity.this, 0, new Intent("net.accella.sheduleexample.UnsilenceBroadcastReceiver"), PendingIntent.FLAG_UPDATE_CURRENT);
//schedule time for pending intent, and set the interval to day so that this event will repeat at the selected time every day
am.setRepeating(AlarmManager.RTC_WAKEUP, sixCalendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, sixPI);
}

});

Thats a lot so let me explain whats going on here. We are creating the intent to run the silence code. However we are making it a pending intent to run at a future date. Next we get an instance of “Alarm Manager” which will take a calendar object and a Pending Intent and run the code accordingly. You may also notice I’ve set the interval to “AlarmManager.INTERVAL_DAY” which repeats this alarm daily, however you can also set alarms that repeat every 15 minutes, every half hour, every hour, and every half day. You can also set this property as NULL to have it fire once (no repeat).

 

And finally we write the code for the second button (unschedule) which removes the schedule :


unschedule.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
//get Alarm manager instance
AlarmManager am = (AlarmManager) LayoutActivity.this.getSystemService(ALARM_SERVICE);
//build intent for midnight
PendingIntent midnightPI = PendingIntent.getService(LayoutActivity.this, 0, new Intent("net.accella.sheduleexample.SilenceBroadcastReceiver"), PendingIntent.FLAG_UPDATE_CURRENT);
//cancel it
am.cancel(midnightPI);

//build intent for 6 AM
PendingIntent sixPI = PendingIntent.getService(LayoutActivity.this, 0, new Intent("net.accella.sheduleexample.UnsilenceBroadcastReceiver"), PendingIntent.FLAG_UPDATE_CURRENT);
//cancel it
am.cancel(sixPI);
}

});

Hank McLaughlin

Hank McLaughlin

Hank specializes in mobile software development, specifically software for the iPhone. He also has a background in mac development and development for webOS devices and started developing software in C while attending Virginia Tech. Since then he has taught himself C++, objective C, PHP, javascript and css.

7 Responses

  1. Thats awsome sir… actually this app is verry usefull.. thanks for explaining it so well…

  2. nice code..it help me to resolve the issue in my app.i am trying to solve since 3 days..thank you so much..for this awsome code..

  3. Sir i want to do it in silent mode after receiving signal of particular frequency. how to do it?

  4. Thanks sir for useful tutorial, but I have a little confusion that what will be written in the manifest file? If anyone have complete code of this application kindly mail it to me at [redacted] …. Millions of thanks in Advance.

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories

Search

Recent Posts

Most Common Tags