Skip to main content

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 <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>  permission to use the AlarmManger in our application, so do not forget to declare the permission in manifest file

AndroidManifest file :-

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.learnandroideasily.blogspot"
    android:versionCode="1"
    android:versionName="1.0" >

            <uses-sdk android:minSdkVersion="8"
                             android:targetSdkVersion="17" />
             <!-- permission required to use Alarm Manager -->
            <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
            <!-- permission required to Send SMS -->
            <uses-permission android:name="android.permission.SEND_SMS"/>
   
            <application
                     android:icon="@drawable/ic_launcher"
                     android:label="Demo App" >
                    <activity
                               android:name=".MainActivity"
                               android:label="Demo App" >
                              <intent-filter>
                                           <action android:name="android.intent.action.MAIN" />
                                           <category android:name="android.intent.category.LAUNCHER" />
                              </intent-filter>
                   </activity>
               <!-- Register the Alarm Receiver -->
                   <receiver android:name=".AlarmReciever"/>
           
         </application>
</manifest>


main.xml








<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center_vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@+id/textView1"
        android:gravity="center_horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Alarm Manager Example"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button1"
        android:layout_marginTop="25dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Schedule The Alarm"
        android:onClick="scheduleAlarm"/>

</LinearLayout>
mainactivity.java


public class MainActivity extends Activity
{
       @Override
       public void onCreate(Bundle savedInstanceState)
      {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
      }

    public void scheduleAlarm(View V)
    {
            // time at which alarm will be scheduled here alarm is scheduled at 1 day from current time,
            // we fetch  the current time in milliseconds and added 1 day time
            // i.e. 24*60*60*1000= 86,400,000   milliseconds in a day       
            Long time = new GregorianCalendar().getTimeInMillis()+24*60*60*1000;

            // create an Intent and set the class which will execute when Alarm triggers, here we have
            // given AlarmReciever in the Intent, the onRecieve() method of this class will execute when
            // alarm triggers and
            //we will write the code to send SMS inside onRecieve() method pf Alarmreciever class
            Intent intentAlarm = new Intent(this, AlarmReciever.class);
       
            // create the object
            AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

            //set the alarm for particular time
            alarmManager.set(AlarmManager.RTC_WAKEUP,time, PendingIntent.getBroadcast(this,1,  intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
            Toast.makeText(this, "Alarm Scheduled for Tommrrow", Toast.LENGTH_LONG).show();
        
    }
}


AlarmReciever.java




public class AlarmReciever extends BroadcastReceiver
{
         @Override
            public void onReceive(Context context, Intent intent)
            {
                    // TODO Auto-generated method stub
               
                   
                      // here you can start an activity or service depending on your need
                     // for ex you can start an activity to vibrate phone or to ring the phone  
                                    
                    String phoneNumberReciver="9718202185";// phone number to which SMS to be send
                    String message="Hi I will be there later, See You soon";// message to send
                    SmsManager sms = SmsManager.getDefault();
                    sms.sendTextMessage(phoneNumberReciver, null, message, null, null);
                    // Show the toast  like in above screen shot
                    Toast.makeText(context, "Alarm Triggered and SMS Sent", Toast.LENGTH_LONG).show();
             }
     
}


Comments

Popular posts from this blog

Home automation using Arduino Board

Today’s homes require sophistication control in its different gadgets which are basically electronic appliances. This has revolutionized the area of home automation with respect to an increased level of affordability and simplicity through the integration of home appliances with smart phone and tablet connectivity. Smart phones are already feature-perfect and can be made to communicate to any other devices in an adhoc network with a connectivity options like Bluetooth . With the advent of mobile phones, Mobile applications development has seen a major outbreak. Utilizing the opportunity of automating tasks for a smart home, mobile phone commonly found in normal household can be joined in a temporary network inside a home with the electronic equipments. Android, by Google Inc. provides the platform for the development of the mobile applications for the Android devices . Home automation system is a mobile application developed using Android targeting its vast market which will be benefi...

Beyond the Chat Box: How Agentic AI is Quietly Automating Your Customer's Next Purchase Journey

We have all been there. You land on an e-commerce storefront, a generic chat bubble pops up in the bottom right corner, and a robot named "ShopBot" asks if you need help finding shoes. You type in a specific problem—say, your last order arrived damaged and you need a replacement shipped to a temporary holiday address. The chatbot instantly gives you a link to the standard returns policy page. You are stuck in a dead-end conversational loop. For years, e-commerce has leaned on these traditional chatbots to deflect customer service tickets. But in 2026, a fundamental structural shift is happening. E-commerce is moving away from reactive, prompt-based chat interfaces and entering the era of Agentic Commerce (McKinsey, 2026). If you are still treating AI as a glorified answering machine, you aren't just falling behind—you are optimizing for an outdated version of the internet. Here is what separates traditional chatbots from agentic AI systems, and how the shift is changi...

Tips for Building Android Mobile and Tablet Apps

1. Check Popular Apps Before you actually start writing your own apps, you definitely want to resear ch the market if there are already exisiting apps providing the planned features. If there are, then take notes about what they provide and ask yourself if you are confident in doing better. 2. Model After Existing Sample Codes If you installed the Android SDK, there are several great sample projects inside sdk/extras/android/support/samples showcasing almost all the user interface examples and major functionalities, which you can use as templates to build and modify for your apps. 3. Avoid Undocumented APIs Some code worked at first but after a few Android SDK updates, it stopped working. 4. Layout for Device Orientation and Various Screen Sizes Since devices have embedded orientation sensors and they are usually defaulted to automatic mode, we need to account for that to provide a better user experience.