Thursday, October 14, 2010

Schedule a repeating alarm

In the last exercise "A simple example of Alarm Service, using AlarmManager" implement a Alarm Service with a one shoot alarm. It will be modified here to have a repeating alarm in duration of 5 seconds.

Just modify the code alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent) in AndroidAlarmService class to alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5*1000, pendingIntent).

public void onClick(View arg0) {
// TODO Auto-generated method stub

Intent myIntent = new Intent(AndroidAlarmService.this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(AndroidAlarmService.this, 0, myIntent, 0);

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
//alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5*1000, pendingIntent);

Toast.makeText(AndroidAlarmService.this, "Start Alarm", Toast.LENGTH_LONG).show();
}});


public void setRepeating (int type, long triggerAtTime, long interval, PendingIntent operation), like set(int, long, PendingIntent), except you can also supply a rate at which the alarm will repeat.

Related article:
- App Widget using Alarm Manager

11 comments:

sathish said...

Hi,
I am newby to android development. I am developing an alarm application in android,
I need to create a single repeat alarm which will repeat in selected multiple days in a week like ,if i select every monday ,every tuesday,every wednesday then the created alarm should repeat in those three days in every week until it will be canceled.

Can you help me in this?

thanks in advance
s.avunoori
sathya.sri69@gmail.com

Erik said...

hello sathya,

I think you can set the first alarm fire using alarmManager.set, then set the subsequence using alarmManager.setRepeating in the first fire.

Unknown said...

Hi,

I need to display the Notification based on the time from the database (Eg: 10AM,12 AM,etc.. )even if the app is not running.. Database will contain many dates with times. If the time reaches i need to display the notificaiton.How can I do that. Lease provide any samples or suggestions

Levi said...

Hi, is this alarm supposed to work even if the device is powered off?

I'm using a tablet, Coby Kyros MID 7016-4G, and it doesn't turn on...

Unknown said...

hi,
i tried using this: alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5*1000, displayIntent);

however it repeat like every 5 sec(think so).. but i don't want that..
what i want is repeat the same time
everyday.. means i set the alarm once, and it will alarm the same time everyday eg. alarm clock

is there anything to do with 5*1000 on the code?

Unknown said...

Nur Adilah you want the alarm to repeat every 24h, so you should set the repeating time to:
24*3600*1000
(24 hours, every hour with 3600 secs, 1000 times because you have to specify the repeating time in milis)

I think that should work :)

Anonymous said...

i tried using this: alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5*1000, displayIntent);

Why i cannot cancel the alarm ?
The system keep on toast message.
Anyone can help on this ?

Regards,
Chua
chuasw23@gmail.com

Anonymous said...

Thanks. It saved me a lot of time.

Anamika said...

I want to make an alarm which rings at 9:30 pm everynight. And also displays a message saying "Have Dinner" .
Can u please tell me how to do it ? Its kinda urgent. Thanks :)

anamikabarbie@yahoo.co.in

Erik said...

please read Create alarm set on a specified time, using AlarmManager and BroadcastReceiver.

Anonymous said...

Hi Thanks a million. I'm a beginner level for android and It is very helpful for me.