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);
try to change the following things
inexactrepeating
torepeating
calendar.add(Calendar.DAY_OF_MONTH, 1);
tocalendar.add(Calendar.DATE,1);
let me know if this works.
you need to implement services: https://developer.android.com/guide/components/services.html
Moreover, you may find many tutorials on implementing services in app