0

the alarm doesn't work at the specified time (20 December 2018, 12:10:02)

what is the problem with this code?

Calendar c = Calendar.getInstance();
c.set(Calendar.MONTH,12);
c.set(Calendar.YEAR,2018);
c.set(Calendar.DAY_OF_MONTH,20);
c.set(Calendar.HOUR_OF_DAY,12);
c.set(Calendar.MINUTE,10);
c.set(Calendar.SECOND,2);

Intent intent = new Intent(ProfileList.this, IntentBroadcastedReceiver.class);
PendingIntent pintent = PendingIntent.getService(ProfileList.this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), 30*1000, pintent);
Rafid
  • 641
  • 1
  • 8
  • 20
  • 1
    What doesn't work? And in the title you mention alarm manager, but your code only includes the Calendar instance. Isn't there some part of your current code missing for us to get the full picture? – Kevin Cruijssen Jun 29 '18 at 09:14
  • This same question is answered here also. https://stackoverflow.com/questions/3052149/using-alarmmanager-to-start-a-service-at-specific-time – Rugved Marathe Jun 29 '18 at 09:17
  • 1
    @RugvedMarathe That question was 8 years ago. A better api was introduced since then. – Viacheslav Shalamov Jun 29 '18 at 09:20
  • @RugvedMarathe it seems the problem is when setting up the calendar – Rafid Jun 29 '18 at 09:28

3 Answers3

4

Use java.time api instead:

The LocalDateTime is used to represent a combination of date and time.

import java.time;

LocalDateTime ldt = LocalDateTime.of(2018, Month.DECEMBER, 20, 12, 10);
// or just parse it from string:
LocalDateTime ldt2 = LocalDateTime.parse("2018-12-20T12:10:00");

// after that, you can get millis for your zone this way:
ZonedDateTime zdt = ldt.atZone(ZoneId.of("America/Los_Angeles"));
long currentTimeMillis = zdt.toInstant().toEpochMilli();

//... your code
alarm.setRepeating(AlarmManager.RTC_WAKEUP, currentTimeMillis, 30*1000, pintent);

See this guide for datails: http://www.baeldung.com/java-8-date-time-intro

For Android earlier than 26, use the ThreeTenABP project that adapts the ThreeTen-Backport project that back-ports much of the java.time functionality to Java 6 & 7.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Viacheslav Shalamov
  • 4,149
  • 6
  • 44
  • 66
2

I got it, month started from 0. it should be:

c.set(Calendar.MONTH,11); //December
Rafid
  • 641
  • 1
  • 8
  • 20
  • 1
    Why don't you use Calendar.DECEMBER? – Zun Jun 29 '18 at 09:39
  • 1
    It’s one, no, it’s *two* of the troubles with the old and outdated `Calendar` class: (1) it uses 0-based months (2) with standard settings it doesn’t object when you set month or any other field to an invalid value. Instead use `java.time`. See [Viacheslav Shalamov’s answer](https://stackoverflow.com/a/51098314/5772882). – Ole V.V. Jun 29 '18 at 10:01
0

The following code worked for me :

 // for Alarm 15/09/2018 at 12.00   
 Calendar myAlarmDate = Calendar.getInstance();
 myAlarmDate.setTimeInMillis(System.currentTimeMillis());
 myAlarmDate.set(2012, 09, 15, 12, 00, 0);
 AlarmManager alarmManager = (AlarmManager)

 context.getSystemService(Context.ALARM_SERVICE);

 Intent _myIntent = new Intent(context,AlarmReceiverNotificationForEveryMonth.class);
 _myIntent.putExtra("MyMessage","HERE I AM PASSING THEPERTICULAR MESSAGE WHICH SHOULD BE SHOW ON RECEIVER OF ALARM");
 PendingIntent _myPendingIntent = PendingIntent.getBroadcast(context, 123, _myIntent,PendingIntent.FLAG_UPDATE_CURRENT);
 alarmManager.set(AlarmManager.RTC_WAKEUP, myAlarmDate.getTimeInMillis(),_myPendingIntent);

Hope it help you.