2

How to start and end a function using Alarm-manager everyday at specific time

For eg StartTime : 07:00 End Time :15:00

 Calendar cur_cal = new GregorianCalendar();
 cur_cal.setTimeInMillis(System.currentTimeMillis());//set the current time and date for this calendar

 Calendar cal = new GregorianCalendar();
 cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
 cal.set(Calendar.HOUR_OF_DAY, 07);
 cal.set(Calendar.MINUTE, 00);
 cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND));
 cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
 cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
 cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
Intent intent = new Intent(this, MyBroadcastReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(
            this.getApplicationContext(), 234324243, intent, 0);
 AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
 alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30*1000, pendingIntent );
Ashraf
  • 3,114
  • 3
  • 23
  • 22

1 Answers1

0

Set one Alarm for starting at 7:00 and one for stopping at 15:00, using Intent-Extras to specify the correct action (starting or stopping).

Also, compare Using Alarmmanager to start a service at specific time

Community
  • 1
  • 1
koljaTM
  • 10,064
  • 2
  • 40
  • 42