我尝试每天使用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
。
要在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);