Search code examples
androidalarmmanagerandroid-alarms

Alarm Manager doesn't go off


I'm building an Alarm Application. for it, I'm need to supply a time as input. I'm trying following code but alarm doesnt' trigger. I searched online and modified the code much alot but still no luck.

Can someone help me out. Thanks in advance.

Intent intent = new Intent(NapAppActivity.this, OnetimeAlarmReceiver.class);
 PendingIntent pendingIntent = PendingIntent.getBroadcast(NapAppActivity.this, 1,  intent, 0);

     Calendar cur_cal = new GregorianCalendar();
    cur_cal.setTimeInMillis(System.currentTimeMillis());

    Calendar cal = new GregorianCalendar();
    cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
    cal.set(Calendar.HOUR_OF_DAY, 12);
    cal.set(Calendar.MINUTE, 17);
    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));

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

Solution

  • I think that you should use cal=Calendar.getInstance() to get the current date/time instead of using GregorianCalendar constructor.

    Then, just set HourOfDay and Minutes, do not touch the other fields.