osayilgan
osayilgan

Reputation: 5893

Set repetitive alarm between exact times in a day

I'm working on an application in Android and I need to create a reminder which will run everyday from 09:00 to 22:00 (Let's say once in an hour or once in two hours) until I cancel it. I have a code below where I create a reminder with the given exact date and with the given id. So in that way I can cancel the reminder later on with the defined id.

Any Idea how to do it ?

    public void setAlarm(Context context, Calendar calendar, int alarmId) {
        Intent intent = new Intent(context, AlarmReceiver.class);           
        PendingIntent sender = PendingIntent.getBroadcast(context, alarmId, intent, 0);
        // Get the AlarmManager service
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
    }

Upvotes: 0

Views: 850

Answers (1)

Khantahr
Khantahr

Reputation: 8548

If I understand your question correctly, when you receive the alarm in your BroadcastReceiver, then set another one for an hour later, or whatever interval you want. If that next alarm would be beyond 22:00, then make it for 09:00 the next day. A repeating alarm would not work for what you proposed, so just use set like you did in the example you gave.

Upvotes: 2

Related Questions