Android AlarmManager class allows you to access the system alarm services. It lets you to schedule an application to run at specific time. The feature works even when your phone is not running.
Android AlarmManager holds a CPU wake lock to make sure phone does not go to sleep mode till the broadcast is handled. In this tutorial, we will explain you how to schedule an application using Android AlarmManager class.
In order to make a schedule, you have to create an instance of the AlarmManager. The syntax is given below:
Ex:
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
AlarmManager is always registered with the intent. When an alarm goes off, the Intent which has been registered with AlarmManager is broadcasted by the system automatically. This intent starts the target application if it is not running.
AlarmManager has the following methods:
• Set(): Schedules an alarm for one time.
• setRepeating(): Schedules an alarm with exact repeating time.
• setTime(): Sets the system’s wall clock time
• setTimeZone(): Sets the system’s default time zone.
In order to start an AlarmManager, you should trigger the BroadcastReceiver repeatedly. In the onReceive() method of OnAlarmReceiver, you should start another activity(MainActivity.java) indirectly such that the activity(MainActivity.java) will be started at the scheduled interval(2 secs).
Create MainActivity.java under src/<your packagename>.
MainActivity.java:
[java]
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(getBaseContext(), OnAlarmReceive.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
MainActivity.this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Getting current time and add the seconds in it(2 sec)
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 2);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
MainActivity.this.finish();
}
}
[/java]
Create OnAlarmReceiver, a BroadcatReceiver class, under src/<your packagename>.
OnAlarmReceive.java:
[java]
public class OnAlarmReceive extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Start the MainActivity
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
[/java]
Create activity_main.xml under res/layout folder.
activity_main.xml:
[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"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textColor="#009ACD"
android:text="AlarmManager Example" />
</RelativeLayout>
[/xml]
AndroidManifest.xml:
[xml]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.spl.myproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.spl.myproject.MainActivity"
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=".OnAlarmReceive" />
</application>
</manifest>
[/xml]
Output: