AlarmManager在错误的时间触发

时间:2015-10-07 04:55:47

标签: android alarmmanager android-alarms repeatingalarm

我尝试每天使用AlarmManager执行一次任务,如下面的代码所示。

 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解决了这个问题。但是在代码中,我在12:30 AM上设置了警报。但是上面的代码会在06:30 AM触发警报。我做错了什么,请通知我。我在12:15 AM的其他应用中设置了警报。但它也会在错误的时间触发,即06:15 AM。请帮帮我。

1 个答案:

答案 0 :(得分:1)

要在12.30 AM设置闹钟,您可以尝试使用此代码。

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

而不是

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