Set Alarm Manager for specific days in week - android

I am working on an android app for college work and I am trying to set a repeating alarms for specific days (or everyday. both options are possible).
Here is my code for this:
public void createAlarms(List<Reminders> medWreminders) {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
for (int i = 0; i < medWreminders.size(); i++) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
Calendar setcalendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, medWreminders.get(i).reminder.getHour());
calendar.set(Calendar.MINUTE, medWreminders.get(i).reminder.getMinutes());
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.DAY_OF_WEEK, medWreminders.get(i).reminder.getDayInt());
// cancel already scheduled reminders
notificationHelper.cancelNotification(this, medWreminders.get(i).reminder.getId());
if (calendar.before(setcalendar)) {
calendar.add(Calendar.DATE, 1);
}
// enable receiver
ComponentName receiver = new ComponentName(this,
AlertReceiver.class);
PackageManager pm = this.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
Intent intent = new Intent(this, AlertReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,
medWreminders.get(i).reminder.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY * 7, pendingIntent);
}
It's important to mention that if the alarm is for daily basis, instead of setting one repeating alarm on a daily interval, (for other reasons) I'm setting 7 different alarms based on weekly interval- one for each day (which, I think, should behave the same). And remember, it is also for specific days in week.
So the problem is: if I set alarms- one for each day- at the same day it works but on the next day nothing happens.
Does anyone know the problem? Thanks!

Related

Repeating alarm not triggering in android at every 12 am daily

I am using the following code to set a repeating alarm in android to trigger at every 12am but it not getting trigger even at one.
I need this alarm to trigger from next day 12 am hence I added one date in calendar object.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 00);
calendar.set(Calendar.MINUTE,00);
calendar.set(Calendar.SECOND,00);
calendar.add(Calendar.DATE,1);
Utils.printLog("date from repeating alarm "+calendar.getTime());
Intent startIntent = new Intent(context, RepeatingAlarmReceiver.class);
PendingIntent startPIntent = PendingIntent.getBroadcast(context, 15, startIntent, 0);
AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (alarmMgr != null) {
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, startPIntent);
}
I tested by breakpoint and found that alarmMgr is not null on setting it.
Also the RepeatingAlarmReceiver.class is working.

Android Alarm Scheduling Efficient

I want to scheduler alarms which should trigger daily on given time. They should be 100% consistent. Currently I am using
AlamManager.setInexactRepeating
and it works a day but not next day and itself starts another day , i mean its not consistent.
So what should I use in Android AlarmManager which trigger must trigger daily and should be efficient ?
This code will run the Intent each day on 1 PM or 2 PM. Hope that helps you.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 13); // For 1 PM or 2 PM
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
PendingIntent pi = PendingIntent.getService(context, 0,
new Intent(context, MyClass.class),PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pi);

Android set alarmManager to trigger daily

In my app, I need to start a service at 2:00pm daily. Right now I wrote the code to trigger the alarm once, this code is ran every time I open the app:
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, DownloadReceiver.class);
PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmMgr.cancel(pIntent);
Calendar cal= Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.HOUR_OF_DAY,refreshhour);
cal.set(Calendar.MINUTE,refreshmin);
cal.set(Calendar.SECOND, 0);
// if the scheduler date is passed, move scheduler time to tomorrow
if (System.currentTimeMillis() > cal.getTimeInMillis()) {
cal.add(Calendar.DAY_OF_YEAR, 1);
}
if(android.os.Build.VERSION.SDK_INT>=23) {
alarmMgr.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), pIntent);
}
else{
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pIntent);
}
Q1. I used setAndAllowWhileIdle() for sdk above 23 in case the device is in Doze mode. I cannot find any option in this function that I can set the alarm to repeat every day.
Q2. I also have questions about setInexactRepeating() , normally it is set to repeat every day by setting the parameter INTERVAL_DAY , but in the docs, it says
As of API 19, all repeating alarms will be inexact and subject to
batching with other alarms regardless of their stated repeat interval.
Does this mean INTERVAL_DAY does not work anymore, so how can I set the alarm daily without rerunning this function and reset alarmManager?
Try below code will solve your problem-
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
boolean flag = (PendingIntent.getBroadcast(this, 0,
new Intent("totime.action.string"),
PendingIntent.FLAG_NO_CREATE) != null);
if(!flag)
{
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 14);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Intent intent = new Intent("totime.action.string");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) Data_Graph.this.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),(24*60*60*1000), pendingIntent);
}

Repeating Alarm not working

I know this type of questions are aksed so many times...but please first read my question before down voting or marking as duplicate.
I have referred so many SO questions like this for managing alarm but cant find any solution so i am here.
In my app i want to trigger repeating alarm one at daily 8.00 am and others as user specified.
First i tired to repeat alarm using setRepeating...this is my code
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, 1);
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM,Calendar.AM);
//calendar.set(Calendar.MILLISECOND, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent pintent = new Intent(this, AlarmReceiver.class);
pintent.putExtra("id", 0);
pintent.putExtra("ontime", false);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
pintent, PendingIntent.FLAG_UPDATE_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
Actually the problem is alarm is not firing after long sleep. If it test using changing date and time it fires perfactly....but it stops firing after a night...
So i switched to second option as mentioned in given link. I set alarm using setExact as follow and reschedule it on each fire.
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, 1);
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM,Calendar.AM);
//calendar.set(Calendar.MILLISECOND, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent pintent = new Intent(this, AlarmReceiver.class);
pintent.putExtra("id", 0);
pintent.putExtra("ontime", false);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
pintent, PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(calendar.getTimeInMillis(), pendingIntent);
am.setAlarmClock(alarmClockInfo, pendingIntent);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
am.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
} else
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
But steel i am having same issue. It is not repeating daily. Please also consider this is only the example of my default alarm. There can be N no of exact and repeating alarms as user specified.
Someone also suggest me to use service rather than alarm manager, but my service might be killed so i am afraid to use that. One other solution is to check due date and set all alarm daily, but i dont think this is the right way. Is it?
Please suggest me any other method or help me to improve my code. I badly need this to work. Thanx all. My testing devices are HTC One X (4.3), Redmi Prime(6.0) and Mi4i (5.0.2)
I had many problems setting repeating alarms, but in the end this code was working on all my testing devices, maybe this helps:
// set time
Calendar c = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
c.set(Calendar.HOUR_OF_DAY, 8);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
long startUpTime = c.getTimeInMillis();
// startupTime + 24 hours if alarm is in past
if (System.currentTimeMillis() > startUpTime) {
startUpTime = startUpTime + 24 * 60 * 60 * 1000;
}
// initialize alarm
AlarmIntent alarmIntent = new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, alarmIntent, 0);
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, startUpTime, 24 * 60 * 60 * 1000, pendingIntent);

Alarm Manager with 2 pending intents only 1 works?

I have 2 alarms set, one for notifications, and the other one to do some tasks. My problem is that only one alarm seems to work( the notifications service one, the first alarm set). The Other alarm never goes off. Here is my code :
Intent myIntent1 = new Intent(getApplicationContext(), NotificationService.class);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, myIntent1, 0);
AlarmManager alarmManager1 = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Calendar calendar1 = Calendar.getInstance();
calendar1.setTimeInMillis(System.currentTimeMillis());
long frequency1 = 30 * 1000; // in ms
alarmManager1.setRepeating(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), frequency1, pendingIntent);
// Set alarm to fire go to Next day everyday at the same time
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 14); // For 1 PM or 2 PM
calendar.set(Calendar.MINUTE, 57);
calendar.setTimeInMillis(System.currentTimeMillis());
Intent myintent = new Intent(getApplicationContext(), AlarmNextDayService.class);
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
PendingIntent pi = PendingIntent.getService(getApplicationContext(), 11, myintent,0 );
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pi);
Any suggestions are welcome. I have looked atother sources as well nothing works for me till now.
I have also added alarm permisison in the manifest file as the following :
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
Thank you
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 14); // For 1 PM or 2 PM
calendar.set(Calendar.MINUTE, 57);
calendar.setTimeInMillis(System.currentTimeMillis());
You are setting the HOUR_OF_DAY and the MINUTE but them you override that by calling setTimeInMillis(System.currentTimeMillis());
After that you set the alarm with the calendar.getTimeMillis() value which is already in the past, so the alarm is cancelled I think.
You're most likely seeing an issue because a Service is not guaranteed to run when triggered by an alarm, due to power save. If your device is on battery and idle when that alarm goes off, it will not trigger until the next time the device is full on or on AC power. You'll need to use a BroadcastReceiver which holds a wake lock which is then released by the Service when it is done. The WakefulBroadcastReceiver makes this a little easier to handle. This article will help provide more details.

Categories

Resources