Praveen Kumar
Praveen Kumar

Reputation: 567

AlarmManager firing at wrong time

I tried to do some task once a day using AlarmManager as in the following code.

 public void start_alarm(){
    SharedPreferences.Editor editor=alarm_prefs.edit();
    editor.putString("alarm", "alarm");
    editor.apply();
    editor.clear();

    Intent start_alarm=new Intent(MainPage.this,MailService.class);
    PendingIntent pi=PendingIntent.getService(MainPage.this, 100, start_alarm, 0);
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

    Calendar calendar = Calendar.getInstance();

    calendar.add(Calendar.DATE,1);
    calendar.set(Calendar.HOUR_OF_DAY, 12);
    calendar.set(Calendar.MINUTE, 30);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.AM_PM, Calendar.AM);

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),1000*60*60*24,pi);
}

Alarm fired no problem in that. But as in the code I set the alarm on 12:30 AM. But above code triggers the alarm at 06:30 AM. Am I doing anything wrong out there please notify me. I have set Alarms in my other app in 12:15 AM. But it also fires at wrong time i.e., 06:15 AM. Please help me with this.

Upvotes: 0

Views: 630

Answers (1)

Rajesh Jadav
Rajesh Jadav

Reputation: 12861

For setting alarm at 12.30 AM you can try this code.

// Set the alarm's trigger time to 12.30 A.M
calendar.add(Calendar.DATE,1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 0);

instead of

calendar.add(Calendar.DATE,1);
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);

Upvotes: 1

Related Questions