Android AlarmManager

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Android AlarmManager

Android AlarmManager allows you to access system alarm.

By the help of Android AlarmManager in android, you can schedule your application to run at a specific time in the future. It works whether your phone is running or not.

The Android AlarmManager holds a CPU wake lock that provides guarantee not to sleep the phone until broadcast is handled.

Android AlarmManager Example

Let's see a simple AlarmManager example that runs after a specific time provided by user.

activity_main.xml

You need to drag only a edittext and a button as given below.

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <EditText  
  8.         android:id="@+id/time"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_alignParentTop="true"  
  13.         android:layout_marginTop="28dp"  
  14.         android:ems="10"  
  15.         android:hint="Number of seconds"  
  16.         android:inputType="numberDecimal" />  
  17.   
  18.     <Button  
  19.         android:id="@+id/button1"  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:layout_alignRight="@+id/time"  
  23.         android:layout_below="@+id/time"  
  24.         android:layout_marginRight="60dp"  
  25.         android:layout_marginTop="120dp"  
  26.         android:text="Start" />  
  27.   
  28. </RelativeLayout>  

Activity class

The activity class starts the alarm service when user clicks on the button.

  1. package com.example.alarmexample;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlarmManager;  
  5. import android.app.PendingIntent;  
  6. import android.content.Intent;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11. import android.widget.EditText;  
  12. import android.widget.Toast;  
  13.   
  14. public class MainActivity extends Activity {  
  15. Button b1;  
  16.   
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.activity_main);  
  21.         b1=(Button) findViewById(R.id.button1);  
  22.       
  23.         b1.setOnClickListener(new OnClickListener() {  
  24.               
  25.             @Override  
  26.             public void onClick(View v) {  
  27.                 // TODO Auto-generated method stub  
  28.                 startAlert();  
  29.             }  
  30.         });  
  31.       
  32. }   public void startAlert() {  
  33.         EditText text = (EditText) findViewById(R.id.time);  
  34.         int i = Integer.parseInt(text.getText().toString());  
  35.         Intent intent = new Intent(this, MyBroadcastReceiver.class);  
  36.         PendingIntent pendingIntent = PendingIntent.getBroadcast(  
  37.                                       this.getApplicationContext(), 234324243, intent, 0);  
  38.         AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);  
  39.         alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()  
  40.                                       + (i * 1000), pendingIntent);  
  41.         Toast.makeText(this"Alarm set in " + i + " seconds",Toast.LENGTH_LONG).show();  
  42.     }  
  43.   
  44. }  

Let's create BroadcastReceiver class that starts alarm.

File: MyBroadcastReceiver.java

  1. package com.example.alarmexample;  

  2. import android.content.BroadcastReceiver;  

  3. import android.content.Context;  

  4. import android.content.Intent;  

  5. import android.media.MediaPlayer;  

  6. import android.widget.Toast;  

  7.   

  8. public class MyBroadcastReceiver extends BroadcastReceiver {  

  9.     MediaPlayer mp;  

  10.     @Override  

  11.     public void onReceive(Context context, Intent intent) {  

  12.         mp=MediaPlayer.create(context, R.raw.alrm   );  

  13.         mp.start();  

  14.         Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show();  

  15.     }  

  16. }  


File: AndroidManifest.xml

You need to provide a receiver entry in AndroidManifest.xml file.

  1. <receiver android:name="MyBroadcastReceiver" >  
  2. </receiver>  

Let's see the full code of AndroidManifest.xml file.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.example.alarmexample"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="16" />  
  10.   
  11.     <uses-permission android:name="android.permission.VIBRATE" />  
  12.   
  13.   
  14.     <application  
  15.         android:allowBackup="true"  
  16.         android:icon="@drawable/ic_launcher"  
  17.         android:label="@string/app_name"  
  18.         android:theme="@style/AppTheme" >  
  19.         <activity  
  20.             android:name="com.example.alarmexample.MainActivity"  
  21.             android:label="@string/app_name" >  
  22.             <intent-filter>  
  23.                 <action android:name="android.intent.action.MAIN" />  
  24.   
  25.                 <category android:name="android.intent.category.LAUNCHER" />  
  26.             </intent-filter>  
  27.         </activity>  
  28.   
  29.         <receiver android:name="MyBroadcastReceiver" >  
  30.         </receiver>  
  31.     </application>  
  32.   
  33. </manifest>  

Output:

android alarmmanager example output 1 android alarmmanager example output 1 android alarmmanager example output 1