Create Repeating Alarm Start After Each 2 Minutes

In This example creating a simple repeating alarm system with the use of AlarmManager. Alarm will start after each 2 mins.


Steps :

  1. Using AlarmManager class to repeating and called a PendingIntent after each 2 min.
2. Call a media file on PendingIntent and start media file.
  3. So after each 2 min AlarmManager will call PendingIntent a media file start.

Project Structure :




File : AndroidMainifest.xml


<?xml version="1.0" encoding="utf-8"?>
    android:versionName="1.0" >
        android:targetSdkVersion="17" />
        android:theme="@style/AppTheme" >
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            android:label="@string/app_name" />


File : src/


Create a alarm with the use of AlarmManager which will call a pending indent RingAlarm after each 2 minutes.


import android.os.Bundle;
import android.os.SystemClock;
import android.content.Intent;
public class AlarmManagerExample extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        try {
            //Create a new PendingIntent and add it to the AlarmManager
            Intent intent = new Intent(this, RingAlarm.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(this,
                12345, intent, PendingIntent.FLAG_CANCEL_CURRENT);
            AlarmManager am =
            am.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),
          } catch (Exception e) {}


File : src/


RingAlarm class create media player and start a audio file after each 2 minutes.


import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
public class RingAlarm extends Activity {
       MediaPlayer mp=null ;
        protected void onCreate(Bundle savedInstanceState) {
                    Button stopAlarm = (Button) findViewById(;
                    mp = MediaPlayer.create(getBaseContext(),;
                    stopAlarm.setOnTouchListener(new OnTouchListener() {
                        public boolean onTouch(View arg0, MotionEvent arg1) {
                            // TODO Auto-generated method stub
                            return false;
                    playSound(this, getAlarmUri());
                private void playSound(final Context context, Uri alert) {
                    Thread background = new Thread(new Runnable() {
                        public void run() {
                            try {
                            } catch (Throwable t) {
                                Log.i("Animation", "Thread  exception "+t);
                protected void onDestroy() {
                }               //Get an alarm sound. Try for an alarm. If none set, try notification,
                    //Otherwise, ringtone.
                private Uri getAlarmUri() {
                    Uri alert = RingtoneManager
                    if (alert == null) {
                        alert = RingtoneManager
                        if (alert == null) {
                            alert = RingtoneManager
                    return alert;


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s