每天凌晨5点发出警报 [英] Alarm everyday at 5AM morning

查看:129
本文介绍了每天凌晨5点发出警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个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);


推荐答案

尝试更改以下内容


  1. 将警报类型从不准重复更改为重复

  2. calendar.add(Calendar.DAY_OF_MONTH,1); 更改为 calendar.add(日历。日期,1);

  1. change the alarm type from inexactrepeating to repeating
  2. change calendar.add(Calendar.DAY_OF_MONTH, 1); to calendar.add(Calendar.DATE,1);

如果有效,请告诉我。

let me know if this works.

这篇关于每天凌晨5点发出警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆