1. AlarmManager in android
2. Setting repeat alarm
3. Defining Alarm BroadcastReceiver
4. Schedule and cancel alarm from activity
5. Starting alarm service on device reboot
6. Declaring Application Manifest
7. Download source code
In the context of this tutorial we will learn how to create a repeat alarm example in android using AlarmManager class.
1. AlarmManager in android
AlarmManager
is a class in Android which is used access device alarm service. This allow your application to execute certain piece of code on a given scheduled time. This can run outside the lifetime of your application.- Once alarm is scheduled, it will invoke even when your application is not running or even when your device is in sleep.
- An alarm can wake your device, but this consumes more battery. So you have to be careful while scheduling an alarm task which executes more frequently. The schedule frequency should be be set to minimum as possible.
- Once an Alarm Started, this will execute until it is stopped explicitly or until device reboots.
- For example, an AlarmManager is used to initiate long running operations such as syncing data from server once a day.
- AlarmManger help you to fire
Intent
at given intervals - This can be used along with broadcast receivers to start a service to perform network operations.
- It is different form java
Timer
andTimerTask
, as they execute with in application life cycle.
2. Setting repeat alarm
- Android supports two clock types for alarm service. Once is elapsed real time and other is real time clock (RTC)
- Elapsed real time uses the time since device last booted
- Real time clock (RTC) uses UTC time for alarm service clock. RTC is most commonly used for setting alarm service in android.
In this example, we’ll show you to schedule alarm using RTC. The application using a single activity containing three buttons. One button is for starting an alarm service, another to cancel scheduled alarm. The third button is to start the alarm at specified calendar time. For example, if you have set a time for your birthday reminder. It will invoke at 10:30 PM and will repeat on every 30 mins.
Let us have a look into our activity layout file (my_activity.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MyActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="20dp" android:text="@string/hint" android:textAppearance="?android:attr/textAppearanceLarge" /> <Button android:id="@+id/startAlarm" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_below="@+id/textView" android:layout_marginBottom="20dp" android:text="Start Alarm Service" /> <Button android:id="@+id/stopAlarm" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/startAlarm" android:layout_marginBottom="20dp" android:text="Stop Alarm" /> <Button android:id="@+id/stopAlarmAt10" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/stopAlarm" android:text="Stop Alarm at 10:30" /> </RelativeLayout>
3. Defining Alarm BroadcastReceiver
In this example, we are associating alarm service with broadcast receiver. Alarm service will invoke this receiver on scheduled time. For the sake of simplicity we are just showing an toast to user for each time the alarm is invoked. You may write your logic to start a service or download task.
Note: If your alarm has to perform network task, then start a download service inside onRecieve()
method of your alarm broadcast.
package com.javatechig.alarmservice; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class AlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // For our recurring task, we'll just display a message Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show(); } }
4. Schedule and cancel alarm from activity
In the above two steps we have defined activity layout and alarm broadcast receiver. Let us have a look into MainActivity.java file. Inside MyActivity class, we have defined three simple methods start(), stop() and startAt10(). The start() method schedule the alarm, cancel() method cancel the scheduled alarm, and startAt!0() method will start the alarm at 10:30 PM with a fixed 20 minutes interval.
package com.javatechig.alarmservice; import android.app.Activity; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Toast; import java.util.Calendar; public class MyActivity extends Activity { private PendingIntent pendingIntent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); /* Retrieve a PendingIntent that will perform a broadcast */ Intent alarmIntent = new Intent(MyActivity.this, AlarmReceiver.class); pendingIntent = PendingIntent.getBroadcast(MyActivity.this, 0, alarmIntent, 0); findViewById(R.id.startAlarm).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { start(); } }); findViewById(R.id.stopAlarm).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { cancel(); } }); findViewById(R.id.stopAlarmAt10).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startAt10(); } }); } public void start() { AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); int interval = 8000; manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent); Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show(); } public void cancel() { AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); manager.cancel(pendingIntent); Toast.makeText(this, "Alarm Canceled", Toast.LENGTH_SHORT).show(); } public void startAt10() { AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); int interval = 1000 * 60 * 20; /* Set the alarm to start at 10:30 AM */ Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.set(Calendar.HOUR_OF_DAY, 10); calendar.set(Calendar.MINUTE, 30); /* Repeating on every 20 minutes interval */ manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 20, pendingIntent); } }
5. Starting alarm service on device reboot
As discussed earlier, once an alarm service is started, it execute until it is explicitly stopped or until device reboots. This means that, if your device is restarted then your alarm is stopped. To avoid such situatation, you have to restart your alarm service as soon as device boot completes. Below code snippet will help you to start alarm service once device reboots.
package com.javatechig.alarmservice; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; /** * @author Nilanchala * <p/> * Broadcast reciever, starts when the device gets starts. * Start your repeating alarm here. */ public class DeviceBootReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) { /* Setting the alarm here */ Intent alarmIntent = new Intent(context, AlarmReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0); AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); int interval = 8000; manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent); Toast.makeText(context, "Alarm Set", Toast.LENGTH_SHORT).show(); } } }
6. Declaring Application Manifest
To start your alarm on device reboot, you have to register your above declared DeviceBootReciever
class in your application manifest. This also need android.permission.RECEIVE_BOOT_COMPLETED
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.javatechig.alarmservice" > <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <!-- Permission to start Alarm on device reboot --> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <activity android:name=".MyActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".AlarmReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver> <!-- Will not be called unless the application explicitly enables it --> <receiver android:name=".DeviceBootReceiver" android:enabled="false"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver> </application> </manifest>