How to use Alarm Service in android?

By | July 30, 2011

Today in this tutorial I will show you how to use AlarmService in android.
For this you have to first create a project named AlarmDemo and copy this code into it.

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class AlarmDemo extends Activity {
	Toast mToast;
		protected void onCreate(Bundle savedInstanceState) {

	        Button button = (Button)findViewById(;


	    private OnClickListener mOneShotListener = new OnClickListener() {
	        public void onClick(View v) {

	            Intent intent = new Intent(AlarmDemo.this, MyAlarmReceiver.class);
	            PendingIntent sender = PendingIntent.getBroadcast(AlarmDemo.this,
	                    0, intent, 0);

	            // We want the alarm to go off 10 seconds from now.
	            Calendar calendar = Calendar.getInstance();
	            calendar.add(Calendar.SECOND, 10);

	            // Schedule the alarm!
	            AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
	            am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);

	            // Tell the user about what we did.
	            if (mToast != null) {
	            mToast = Toast.makeText(AlarmDemo.this,"Alarm Started",

We use the AlarmManager class for creating an Alarm service.
Here we are setting the time to 10 seconds so after 10 seconds a Toast pops up.
For receiving the Broadcast we have to create a class the extends the BroadcastReceiver class on which the onReceive function gets called when the service ends.
So create a class named “” and copy this code into it.

package pack.coderzheaven;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MyAlarmReceiver extends BroadcastReceiver
    public void onReceive(Context context, Intent intent)
        Toast.makeText(context, "Alarm Received after 10 seconds.", Toast.LENGTH_SHORT).show();

This is the layout file main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
	android:orientation="vertical" android:padding="4dip"
    android:layout_width="fill_parent" android:layout_height="fill_parent">

        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:text="Alarm Demo"/>

    <Button android:id="@+id/one_shot"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="Start Alarm">
        <requestFocus />

This class will be called after 10 seconds.

Now the important thing for receiving the Brodcast events you have to register with the AndroidManifest.

This is the AndroidManifest file for the above code

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AlarmDemo"
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
	<receiver android:name=".MyAlarmReceiver" android:process=":remote" />

Alarm Demo

Alarm Demo

Alarm Demo

Alarm Demo

6 thoughts on “How to use Alarm Service in android?

  1. Pingback: How to setUp a repeating Alarm in Android? | Coderz Heaven

  2. Allan J.

    Where can I embed this code? Also, what services it can give if I embed this code to my phone? There are alarm monitoring services that i’ve been looking for, one thing is this.

    1. James Post author

      @Allan:- You can modify this code to do whatever you want. YOu can embed this code in another application that needs an alarm service or something etc.

  3. Pingback: Conext alarm | Trkiyecumhuriy

  4. Pingback: Lista temelor si calendarul aferent | Aplicatii Mobile

  5. Pingback: Lista echipe AMA 2014-2015 (in curs de actualizare) | Aplicatii Mobile

Leave a Reply

Your email address will not be published. Required fields are marked *