Alarm everyday at 5AM morning

送分小仙女□ 提交于 2019-12-06 16:24:24

问题


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:


try to change the following things

  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.




回答2:


you need to implement services: https://developer.android.com/guide/components/services.html

Moreover, you may find many tutorials on implementing services in app



来源:https://stackoverflow.com/questions/47324975/alarm-everyday-at-5am-morning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!