28

I'm trying to make a Schedule.

It should run every day at 1pm or 2pm...

At the moment I can only make it run Every 10Sec or 10min...

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);

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

Thanks

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
Paul
  • 321
  • 1
  • 5
  • 8

2 Answers2

102

This code will run the Intent each day on 1 PM or 2 PM

Calendar calendar = Calendar.getInstance();

calendar.set(Calendar.HOUR_OF_DAY, 13); // For 1 PM or 2 PM
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
PendingIntent pi = PendingIntent.getService(context, 0,
            new Intent(context, MyClass.class),PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                                AlarmManager.INTERVAL_DAY, pi);
ol_v_er
  • 27,094
  • 6
  • 48
  • 61
mcanti
  • 1,964
  • 2
  • 17
  • 14
  • 22
    You may want to add calendar.add(Calendar.DAY_OF_YEAR, 1); to avoid firing the alarm immediately. – ben Jul 21 '13 at 03:15
  • You could also add calendar.set(Calendar.MILLISECOND, 0); to be more precise :) – JDJ Jun 10 '14 at 10:47
  • 1
    Would this also run when the application goes background/closed/paused? – Mario Galván Jul 22 '14 at 17:39
  • @MarioGalván yes, until the alarm is cancelled, or the device is rebooted and this code doesn't execute again – Jose_GD Aug 12 '14 at 21:25
  • @JDJ Hmmm I think that won't help much to the alarm's precision, moreover after latest modifications to AlarmManager behavior introduced in KitKat – Jose_GD Aug 12 '14 at 21:31
  • How we can give interval of a month since not every month is of 30 days. – Shajeel Afzal Aug 26 '15 at 10:51
  • And where should this code go? `MainActivity`? When should this be executed? I'm missing the context here. – dialex Mar 04 '16 at 19:47
  • Mine appears everytime i load the app even though i set a time of day... http://stackoverflow.com/questions/40812244/how-to-set-one-alarm-or-multiple-alarms-once-in-notification-and-not-repeat-if – Si8 Nov 25 '16 at 22:03
  • 2
    i have tried it and it works fine but after some days it just stop and app stops firing BroadcastReceiver does any one you know what is going on – Zubair Akber Mar 31 '17 at 07:36
  • @ZubairAkber did you got any solution? – Saeed Jassani Sep 16 '17 at 11:21
  • No @SaeedJassani i didn't, if you find any kindly share it – Zubair Akber Sep 18 '17 at 12:26
-10

This will alarm every day.

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,  AlarmManager.INTERVAL_DAY , pendingIntent);
Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103