Alarm Manager

 

Whatever we code in android are made to be executed at that moment. But what if we want to trigger an event or task or an action in a time in future; may be after 15 mins , and hour or a day. To help ease this process the class AlarmManager is provided to us.

Lets get started and learn more about alarms.

Alarm Manager

 

Alarms:

What is an alarm? Alarms are the mechanism to schedule an intent in specific time in future.

Why do we need an alarm? We need an alarm to perform an action in future even if the application is not running.

Have we ever noticed that even if we exit the bluetooth connection application; it’s visibility timeout will be disabled at the mentioned time? Another obvious example is our clock.

Alarms can we very useful if you want to develop an app that allows certain notifications or triggers in future and perform respective functions.

Alarm Manager:

Quickly we’ll dive into the basics of alarm creation and take a look at the APIs provided for alarms.

  • Get an access to system’s alarm services:
    • getSystemService(ALARM_SERVICE)
  • Creating alarms
    • one-shot alarm
      • void set(int type, long triggerAtMillis, PendingIntent operation)
    • repeating alarm
      • void setRepeating(int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)
    • repeating alarm in exact trigger time
      • void setInexactRepeating(int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)

Lets understand more about these codes;

type: Alarm Type, triggerAtMillis: milliseconds of time when the alarm should be triggered, operation: pending intent that holds what task is to be performed.

Alarm Type:

Alarm type can be explained in two configurations:

  1. Time Interpretation:
    1. RTC (Real Time Clock): Takes reference to the system clock ie. Jan 1, 1970.
    2. Elasped: Takes reference to the mobile boot up time.
  2. Device Sleeping condition:
    1.  RTC & ELAPSED_REALTIME: Wait for the device to Wake up and then trigger the alarm.
    2. RTC_WAKEUP & ELAPSED_REALTIME_WAKEUP: Wake up the device and deliver the intent.

Note: After android API 19 (KitKat) all the alarms are inexact, this is done to save battery life and disable multiple wakeups. By doing so all the alarms are bundled to its near about time and triggered at once.

Sample Code

Create One-shot or Repeating and InExactRepeating Alarm as shown below:

private void createAlarm() {
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    
    // check the type of alarm being triggered
    Bundle bundle = new Bundle();
    bundle.putString("FROM", "RTC");
    Intent intent = new Intent(MainActivity.this, AlarmReciever.class);
    intent.putExtra("BUNDLE", bundle);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

    // set the alarm of RTC type
    alarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + START_TIME, pendingIntent);

    Bundle bundleElapsed = new Bundle();
    bundleElapsed.putString("FROM", "ELAPSED");
    Intent intentElapsed = new Intent(MainActivity.this, AlarmReciever.class);
    intentElapsed.putExtra("BUNDLE", bundleElapsed);
    PendingIntent pendingIntentElapsed = PendingIntent.getBroadcast(this, 0, intent, 0);
    pendingIntentElapsed = PendingIntent.getBroadcast(this, 1, intentElapsed, 0);
    
    // set the alarm of Elapsed type
    alarmManager.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + START_TIME, pendingIntentElapsed);

}

/**
 * See the different alarms being run on both below and above kitkat.
 * You can view how alarm manager bundles all alarm to be executed in nearest time.
 */
private void createRepeatingAlarm() {
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    Bundle bundle = new Bundle();
    bundle.putString("START_TIME", "15Sec");

    Intent intent = new Intent(MainActivity.this, AlarmReciever.class);
    intent.putExtra("BUNDLE", bundle);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

    alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis() + START_TIME, INTERVAL, pendingIntent);

    Bundle newBundle = new Bundle();
    newBundle.putString("START_TIME", "20Sec");

    Intent newIntent = new Intent(MainActivity.this, AlarmReciever.class);
    newIntent.putExtra("BUNDLE", newBundle);

    PendingIntent newPendingIntent = PendingIntent.getBroadcast(this, 1, newIntent, 0);
    alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis() + START_TIME + JITTER, INTERVAL, newPendingIntent);
}

/**
 * See the different alarms being run on both below and above kitkat.
 * You can view how alarm manager bundles all alarm to be executed in nearest time.
 */
private void createRepeatingInexactAlarm() {
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    Bundle bundle = new Bundle();
    bundle.putString("START_TIME", "15Sec");

    Intent intent = new Intent(MainActivity.this, AlarmReciever.class);
    intent.putExtra("BUNDLE", bundle);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

    alarmManager.setInexactRepeating(AlarmManager.RTC, System.currentTimeMillis() + START_TIME, AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent);

    Bundle newBundle = new Bundle();
    newBundle.putString("START_TIME", "20Sec");

    Intent newIntent = new Intent(MainActivity.this, AlarmReciever.class);
    newIntent.putExtra("BUNDLE", newBundle);

    PendingIntent newPendingIntent = PendingIntent.getBroadcast(this, 1, newIntent, 0);
    alarmManager.setInexactRepeating(AlarmManager.RTC, System.currentTimeMillis() + START_TIME + JITTER, AlarmManager.INTERVAL_FIFTEEN_MINUTES, newPendingIntent);
}

Since we have used getBroadcast() method handle the Alarm in your receiver as required.

Advertisements

One thought on “Alarm Manager

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