每天凌晨5点发出警报 [英] Alarm everyday at 5AM morning
问题描述
我正在开发一个Android应用程序,我需要每天早上5点触发警报。我已经使用 Intent
的概念(下面是代码)并将 AlarmManager
放在服务
。但我没有从两个实现中获得问题解决方案。我只是在寻找一个完美的解决方案。如果我第一次打开应用程序,我会在日历
中添加提醒1个月,我想这可能会有效。但如果有更好的解决方案,请提供。无论如何,它应该每天凌晨5点触发警报。我怎样才能实现这个目标?
I am working on an android application where I need to trigger an alarm every morning at 5 am. I have already used the concept of Intent
(below is the code) and putting AlarmManager
in the Service
. But I didnt get the problem solution from both implementations. I am just looking for a perfect solution for this. If I add a reminder to the calendar
for 1 month once the application is opened for the first time I guess that might work. But if there is any better solution for this, then please provide. It should trigger an alarm at 5 AM every day no matter what. How can I achieve this?
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent alarmIntent = new Intent(this, MyStartServiceReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 100, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 5);
calendar.set(Calendar.MINUTE, 5);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
if (System.currentTimeMillis() > calendar.getTimeInMillis()) {
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
alarmManager.cancel(pendingIntent);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
推荐答案
尝试更改以下内容
- 将警报类型从
不准重复
更改为重复
- 将
calendar.add(Calendar.DAY_OF_MONTH,1);
更改为calendar.add(日历。日期,1);
- change the alarm type from
inexactrepeating
torepeating
- change
calendar.add(Calendar.DAY_OF_MONTH, 1);
tocalendar.add(Calendar.DATE,1);
如果有效,请告诉我。
let me know if this works.
这篇关于每天凌晨5点发出警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!