Using the Android Alarm Manager to Schedule Texts

In my current Android application, I needed to send a text message at a specified time. To do this I turned to the AlarmManager. Using this, you can create an alarm for a specified time, and define what action should take place when the alarm is received.

The process is pretty straightforward:

1. Define a BroadcastReceiver to handle whatever actions you want to run when an alarm is received
2. Create an activity in which you create the alarm

Creating the BroadcastReceiver

Broadcast Receiver

When an alarm goes off it must be received by something. The BroadcastReceiver will receive the alarm and perform whatever action is specified. For this example we will extend BroadcastReceiver to send a text message to a number specified by the calling intent.

public class CustomAlarmReceiver extends BroadcastReceiver {

  public void onReceive(Context context, Intent intent) {
   try {
     sendSMS(intent);
   } catch (Exception e) {
     Toast.makeText(context, "ERROR", Toast.LENGTH_SHORT).show();
     e.printStackTrace();
   }
  }

 private void sendSMS(Intent intent){
   Bundle bundle = intent.getExtras();
   SmsManager smsManager = SmsManager.getDefault();

   String smsText = bundle.getString("alarm_message");
   String smsNumber = bundle.getString("number");

  smsManager.sendTextMessage(smsNumber, null, smsText, null, null);

  }
 }

You must override onReceive. Here we call sendSMS(intent) which then pulls the message and number for the text from the extras bundle of the passed intent. The text is then sent using the SMS Manager.

After the receiver is created, it must also be defined in the manifest file for the project.

<receiver android:process=":remote"android:name="CustomAlarmReceiver"></receiver>

Creating the Activity

For our receiver to handle an event, one must first be created. There are several types of alarm events (one-time, recurring, etc) and different ways to create them. This example will be simple. We will create an alarm to send our text 10 minutes from the current time.

Using Android.Calendar you can calculate specific times for an alarm to go off rather than just a general length of time. Ex. You can create an alarm to go off at 1pm each day.

Calendar Reference

For more on types of events and creation see Alarm Manager Reference

public class AlarmExampleActivity extends Activity {

  public static int eventID = 0;

  public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);

   Calendar cal = Calendar.getInstance(); //retrieves a calendar object w/ current time
   cal.add(Calendar.MINUTE, 10); //adds 10 minutes to current time

   Intent alarmIntent = new Intent(this, CustomAlarmReceiver.class);

   //pass extra data to CustomAlarmReceiver intent to be handled when the alarm goes off
   alarmIntent.putExtra("alarm_message", "Hello World");
   alarmIntent.putExtra("number", "Some Phone Number");

   // creates a new PendingIntent using the static variable eventID.
   // using eventID allows you to create multiple events with the same code
   // without a unique id the intent would just be updated with new extras each time its created
   //
   PendingIntent pendingAlarm = PendingIntent.getBroadcast(
   this, eventID, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
   eventID+=1;

  // get the Alarm service and set the alarm with the time to go off and what to do when the
  // alarm is received
  AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
  am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingAlarm);
  }
 }

An important part of this code is the static eventID. Say you have an activity where you want to create multiple alarms to send different texts. If you create the PendingIntent like this

PendingIntent pendingAlarm = PendingIntent.getBroadcast(this, 20000, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);‚Äč

Using the same id (20000) for each intent the FLAG_UPDATE_CURRENT will update the existing intent with the same id with the new extras. This will result in only the most recently created alarm being active.

By using the static variable and incrementing it each time, it allows multiple alarms to be active since the intents are not exactly the same each time they are called.

PendingIntent Reference

If you changed the “number” string from “Some Phone Number” to a valid number, this activity would result in the message “Hello World” being sent to the specified number 10 minutes after the code was fun.

Leave a Reply