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