我有这段代码通过在另一个活动中使用TimePicker和DatePicker设置时间和日期来触发警报一次.
我希望以一种方式对其进行修改,即每当我设置时间和日期时,它每天都会同时触发警报.换句话说,我希望每天都能发出警报.
public class M_ReminderManager {
private Context mContext;
private AlarmManager mAlarmManager;
public M_ReminderManager(Context context) {
mContext = context;
mAlarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
}
public void setReminder(Long reminderId, Calendar when) {
Intent i = new Intent(mContext, Medicines_OnAlarmReceiver.class);
i.putExtra(RemindersDbAdapter.KEY_ROWID_MEDS, (long)reminderId);
PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, i, PendingIntent.FLAG_ONE_SHOT);
mAlarmManager.set(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), pi);
}
}
我尝试过使用setRepeating函数,但我不知道应该如何设置属性
我在代码中使用了这一行而不是set函数,但它不起作用:
mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, when.getTimeInMillis() ,AlarmManager.INTERVAL_DAY , pi);
有人可以帮我吗?
解决方法:
只需修改代码即可
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingIntent)
在AndroidAlarmService类中
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 5*1000, pendingIntent)
并阅读此链接:http://android-er.blogspot.com/2010/10/schedule-repeating-alarm.html