Alarmmanager Android Every Day

Repeat alarm everyday accurately (Alarm manager)

try Adding

calendar.set(Calendar.SECOND,00);

and changing

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, startUpTime,  24*60*60*1000 , pendingIntent);

to

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
startUpTime, AlarmManager.INTERVAL_DAY, pendingIntent);

Run code every day at a specific time - AlarmManager

Note: as of API 19, all repeating alarms are inexact. If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time.

Inexact scheduling means the time between any two successive firings of the alarm may vary. Thats what happening in your case i guess.To overcome the exact time issue i think you should use one shot alarm.But in this case you need to reschedule it for next day. and so on.
You can get a very clear understanding from the Documentation setrepeating , setInexactRepeating.

EDIT:- TO optimizing DOZE mode
In Case you are Using setAndAllowWhileIdle() or setExactAndAllowWhileIdle(),
Then you must read This Document which says :-

Neither setAndAllowWhileIdle() nor setExactAndAllowWhileIdle() can fire alarms more than once per 9 minutes, per app.

Android | setAlartm every day at the same hour | not working

When an alarm is set to a passed date it will trigger immediately, if you don't want that you can create some logic to only start it today if the current time is before 3AM, something like this would work:

    Calendar now = Calendar.getInstance();
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 3);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
Intent intent = new Intent(context, MyBroadcast.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 1253, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (now.before(cal)) {
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
else {
cal.set(Calendar.DAY_OF_YEAR, now.get(Calendar.DAY_OF_YEAR) +1);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}

We created another calendar to use as a reference to the current time, and check if we are before 3AM, if not then only call the alarm a day after.

Alarm manager not triggering everyday

Finally I found a solution to the issue I am facing.
I used alarmMgr.set() method instead of alarmMgr.setRepeating() and when the alarm rings off I set another alarm for the next day. In this way, it will repeat day after day. :)

I still would like to know whats wrong with alarmMgr.setRepeating() method. So, if anyone has any idea, please post the solution.

Below is the code I implemented.

public void setMorningRepeatingTask(Context context, int hour, int minutes, boolean forceScheduleNextDay) {
alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmBroadcastReceiver.class);
intent.putExtra(Constants.KEY_ALARM_TIME, Constants.VALUE_MORNING_ALARM);
alarmIntent = PendingIntent.getBroadcast(context, Constants.MORNING_ALARM_UNIQUE_ID, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minutes);
if (Utility.checkIfTheTimeHasPassed(calendar.getTimeInMillis()) || forceScheduleNextDay) {
calendar.add(Calendar.DATE, 1);
}
alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
Log.i("Morning Alarm", "Alarm is set for " + calendar.get(Calendar.DATE) + " at "
+ calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE));
FileLogger.writeToFile("Alarm Set", "Morning repeating Alarm Set");
FileLogger.writeToFile("Morning Alarm", "Alarm is set for " + calendar.get(Calendar.DATE) + " at "
+ calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE));
}

The above method is for setting the alarm.

if (Utility.checkIfTheTimeHasPassed(calendar.getTimeInMillis()) {
calendar.add(Calendar.DATE, 1);
}

public static boolean checkIfTheTimeHasPassed(long timeInMillis) {
long nowTime = new Date().getTime();
return nowTime > timeInMillis;
}

The above condition will check if the time has already passed, and if it is passed, it will schedule the alarm for the next day.

When the alarm is triggered, onReceive() method of the BroadcastReceiver subclass will be called.

@Override
public void onReceive(Context context, Intent intent) {
FileLogger.writeToFile("Alarms rang off!!", "Alarm rang");

//Add your logic here

setMorningRepeatingTask(context,morningTimeCalender.get(Calendar.HOUR_OF_DAY), morningTimeCalender.get(Calendar.MINUTE), true);
}

In this method after doing the things I want on alarm ring, I reschedule the alarm for the next day using same method.

Android: AlarmManager recuring tasks once a day at midnight

I thought the 1000 was in miliseconds ? it should therefore be 1s ?

It is. However, on Android 5.1 and higher, you cannot schedule a repeating task that frequently. If you try, it will be rounded up to a minute.

how to repeat alarm after 1 day in android

Hopefully below code will help, I used the same in my app. Here the argument passed in AlarmManager class for repeating should be 24*60*60*1000

AlarmManager am = (AlarmManager) ct.getSystemService(Context.ALARM_SERVICE);         
Intent intent1 = new Intent(ct, TimeAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(ct, 0,intent1, PendingIntent.FLAG_CANCEL_CURRENT);

Date curr=new Date();
curr.setHours(h);
curr.setMinutes(m);
c.setTime(curr);
c.set(Calendar.SECOND, 0);

Calendar c1 = Calendar.getInstance();
am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),24*60*60*1000, pendingIntent);

How to use alarmmanager 2 times every day?

You can use an interval of AlarmManager.INTERVAL_DAY / 2 :

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 35);
calendar.set(Calendar.SECOND, 0);
AlarmManager am = (AlarmManager)getApplicationContext().getSystemService (Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY / 2, pi);

but if the time of the day in which you fire your alarm matters you can use two calendar objects :

Calendar cal1 = Calendar.getInstance();
cal1.set(Calendar.HOUR_OF_DAY, 12); //midday
cal1.set(Calendar.MINUTE, 00);
cal1.set(Calendar.SECOND, 00);

Calendar cal2 = Calendar.getInstance();
cal2.set(Calendar.HOUR_OF_DAY, 18);//8pm for example
cal2.set(Calendar.MINUTE, 00);
cal2.set(Calendar.SECOND, 00);

and set your alarm manager :

am.setRepeating(AlarmManager.RTC_WAKEUP, cal1.getTimeInMillis(),cal2.getTimeInMillis(), pi);


Related Topics



Leave a reply



Submit