在android中开发每日闹钟
我有这段代码,通过在另一个活动中使用 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);
有人可以帮助我吗?
I have this piece of code that fire the alarm once by setting a time and date using the TimePicker and the DatePicker in another activity.
I want to modify it in a way that whenever I set a time and a date it will fire the alarm everyday at the same time. In other words I want the alarm to be fired daily.
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);
}
}
I have tried using setRepeating function but I don't know how exactly I should set the attributes
I used this line instead of the set function on the code but it didn't work:
mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, when.getTimeInMillis() ,AlarmManager.INTERVAL_DAY , pi);
Can someone help me with it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
代码修改
只需将
AndroidAlarmService
类中的为并阅读此链接:http://android-er.blogspot.com/2010/10/schedule-repeating-alarm.html
Just modify the code
in
AndroidAlarmService
class toAnd read this link: http://android-er.blogspot.com/2010/10/schedule-repeating-alarm.html
您可以看到此链接:如何每天中午以及每次启动时运行服务
您应该记住在手机重启时重置时间表,链接也包含该代码。
You can see this link: How to run a service every day at noon, and on every boot
You should remember to reset schedule on phone restart, link includes code for that aswell.