这是我的代码,用于设置每月在 4 日重复的闹钟
public void init(Context context) {
Intent intent = new Intent(context, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Get the AlarmManager service
alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
}
public void createAlarmControler(Context context) {
init(context);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 4);
cal.set(Calendar.HOUR_OF_DAY, 10);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
Date now = new Date(System.currentTimeMillis());
if (cal.getTime().before(now)) {
cal.add(Calendar.MONTH, 1);
}
long firstTime = cal.getTime().getTime();
alarmManager.cancel(pendingIntent);
Log.e("", "firstTime: " + firstTime);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, firstTime, 1000 * 60
* 60 * 24 * 30, pendingIntent);
}
public void cancelAlarmControler(Context context) {
init(context);
alarmManager.cancel(pendingIntent);
}