Reputation: 2609
I am developing alarm application for that I need to invoke alarm repeatedly for the same time for all days. I am using the code to invoke the alarm,
c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY,10);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
Intent intent = new Intent(HomeActivity.this, MyAlarmService.class);
PendingIntent pendingIntent = PendingIntent.getService(HomeActivity.this, 123123, intent, 0);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, c.getTimeInMillis(), (24*60*60*1000), pendingIntent);
By using this code I am only invoking present day at 10AM, not for all days. Is there another way to do this?
Upvotes: 1
Views: 2467
Reputation: 45942
In your class MyAlarmService
, when the alarm is on, reset the alarm. You can even send values for MyAlarmService
in the intent when setting the alarm (maybe to stop the alarm)
Upvotes: 0
Reputation: 431
Update time, and ring the alarm for every 5 minutes:
Intent intent = new Intent(this, this.getClass());
PendingIntent pendingIntent =
PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
long currentTimeMillis = System.currentTimeMillis();
long nextUpdateTimeMillis = currentTimeMillis + 5 * DateUtils.MINUTE_IN_MILLIS;
Time nextUpdateTime = new Time();
nextUpdateTime.set(nextUpdateTimeMillis);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, nextUpdateTimeMillis, pendingIntent);
Set when the alarm works for everyday (for example, 8am-18pm)
// if not the time to ring, set to the next day
if (nextUpdateTime.hour < 8 || nextUpdateTime.hour >= 18)
{
nextUpdateTime.hour = 8;
nextUpdateTime.minute = 0;
nextUpdateTime.second = 0;
nextUpdateTimeMillis = nextUpdateTime.toMillis(false) + DateUtils.DAY_IN_MILLIS;
}
else{
// call your alarm
}
Upvotes: 1