在android中开发每日闹钟

发布于 2024-12-18 06:37:37 字数 1047 浏览 1 评论 0原文

我有这段代码,通过在另一个活动中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜警司 2024-12-25 06:37:37

代码修改

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

Just modify the code

alarmManager.set(AlarmManager.RTC_WAKEUP,
    calendar.getTimeInMillis(), pendingIntent)

in AndroidAlarmService class to

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
    calendar.getTimeInMillis(), 5*1000, pendingIntent)

And read this link: http://android-er.blogspot.com/2010/10/schedule-repeating-alarm.html

故笙诉离歌 2024-12-25 06:37:37

您可以看到此链接:如何每天中午以及每次启动时运行服务
您应该记住在手机重启时重置时间表,链接也包含该代码。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文