Android

Alarm Manager App in Android

Many a times we want some task to be performed at some later time in future.
For Example: In SMS Scheduler we want a SMS to be send at some later time, or Task Reminder in which we want to be reminded about a task at a particular time, to implement all these things we use AlramManager class.AlarmManager class provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future.

When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.

The Alarm Manager holds a CPU wake lock as long as the alarm receiver’s onReceive() method is executing. This guarantees that the phone will not sleep until you have finished handling the broadcast.

Once onReceive() returns, the Alarm Manager releases this wake lock. This means that the phone will in some cases sleep as soon as your onReceive() method completes. If your alarm receiver called Context.startService(), it is possible that the phone will sleep before the requested service is launched. To prevent this, your BroadcastReceiver and Service will need to implement a separate wake lock policy to ensure that the phone continues running until the service becomes available.

In this example I will schedule an alarm to send SMS at a particular time in future.

We have two classes :-

1: MainAcitvity: in this class, we will schedule the alarm to be triggered at particular time .

2: AlarmReciever: when the alarm triggers at scheduled time , this class will receive the alarm, and send the SMS.

AlarmReciever class extends BroadcastReceiver and overrides onRecieve() method. inside onReceive() you can start an activity or service depending on your need like you can start an activity to vibrate phone or to ring the phone

Permission Required :-

we need permission to use the AlarmManger in our application, so do not forget to declare the permission in manifest file

AndroidManifest file :-















main.xml

a2

Leave a Reply