简体   繁体   中英

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 ie, 06:15 AM . Please help me with this.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM