Hello everyone…….

Today I am going to show you how to set up a repeating alarm in android. In the previous post I showed you how to set up a simple alarm. An in this post I will show you how to set Up a Repeating Alarm.

For this you need an extra class just to notify that the repeating alarm is called.
Let’a look at the java code.

package pack.coderzheaven;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class AlarmDemo extends Activity {
	Toast mToast;
	  @Override
		protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.main);

	        Button button = (Button)findViewById(R.id.one_shot);
	        button.setOnClickListener(mOneShotListener);
	        button = (Button)findViewById(R.id.start_repeating);
	        button.setOnClickListener(mStartRepeatingListener);
	        button = (Button)findViewById(R.id.stop_repeating);
	        button.setOnClickListener(mStopRepeatingListener);
	    }

	    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.setTimeInMillis(System.currentTimeMillis());
	            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.cancel();
	            }
	            mToast = Toast.makeText(AlarmDemo.this,"Alarm Started",
	                    Toast.LENGTH_LONG);
	            mToast.show();
	        }
	    };

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

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

	            // We want the alarm to go off 5 seconds from now.
	            long firstTime = SystemClock.elapsedRealtime();
	            firstTime += 5*1000;

	            // Schedule the alarm!
	            AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
	            am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
	                            firstTime, 5*1000, sender);

	            // Tell the user about what we did.
	            if (mToast != null) {
	                mToast.cancel();
	            }
	            mToast = Toast.makeText(AlarmDemo.this, "Rescheduled",
	                    Toast.LENGTH_LONG);
	            mToast.show();
	        }
	    };

	    private OnClickListener mStopRepeatingListener = new OnClickListener() {
	        public void onClick(View v) {
	            // Create the same intent, and thus a matching IntentSender, for
	            // the one that was scheduled.
	            Intent intent = new Intent(AlarmDemo.this, RepeatingAlarm.class);
	            PendingIntent sender = PendingIntent.getBroadcast(AlarmDemo.this,
	                    0, intent, 0);

	            // And cancel the alarm.
	            AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
	            am.cancel(sender);

	            // Tell the user about what we did.
	            if (mToast != null) {
	                mToast.cancel();
	            }
	            mToast = Toast.makeText(AlarmDemo.this,"Repeating Alarm Unscheduled.",
	                    Toast.LENGTH_LONG);
	            mToast.show();
	        }
	    };
	}

Here we have three buttons one to set up a simple Alarm that is a one shot alarm and other button is for starting a repeating alarm and the third button for stopping the repeating alarm.

Now let’s see how the layout looks like.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:padding="4dip"
    android:gravity="center_horizontal"
    android:layout_width="fill_parent" android:layout_height="fill_parent">

    <TextView
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:layout_weight="0"
        android:paddingBottom="4dip"
        android:text="Alarm Demo from CoderzHeaven"/>

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

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

     <Button android:id="@+id/stop_repeating"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="Stop Repeating Alarm" />

</LinearLayout>

Now create a new class named “RepeatingAlarm.java” and copy this code into it. This class receives the repeating alarm intent.
The “MyAlarmReceiver.java” file is included in this post. Please copy that also

package pack.coderzheaven;

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

public class RepeatingAlarm extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Toast.makeText(context,"Repeating Alarm Received", Toast.LENGTH_SHORT).show();
    }
}

Here is the AndroidManifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="pack.coderzheaven"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AlarmDemo"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
	<receiver android:name=".MyAlarmReceiver" android:process=":remote" />
	<receiver android:name=".RepeatingAlarm" android:process=":remote" />
    </application>
</manifest>

Now run it and see the result.



 

2 Comments

  1. ishan August 2, 2011 at 12:51 pm

    i have a problem.
    i created simple mapview and i put latitude and longitude and it works wel on my emulator and also some other phones that have android version 2.1
    .. but i tested same application on my phone that have android version 2.3(galaxy s2) it shows only grid view instead if mapp…. can u explain what is the reason,,
    i created applicatoin using android 1.6 ..

     
  2. Pingback: AlarmManager is not repeating - BlogoSfera

Leave a Reply

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

 

 

 

Theme by HermesThemes

Copyright © 2018 CoderzHeaven. All Rights Reserved

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.