I'm creating an alarm using AlarmManager, it works great, but I'd like to test the .setRepeating method, which I setup like this:
Calendar calMon = Calendar.getInstance();
calMon.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
calMon.set(Calendar.HOUR_OF_DAY, tPicker.getCurrentHour());
calMon.set(Calendar.MINUTE, tPicker.getCurrentMinute());
calMon.set(Calendar.SECOND, 0);
calMon.set(Calendar.MILLISECOND, 0);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calMon.getTimeInMillis(), 24 * 60 * 60 * 1000, newIntent(this, Calendar.MONDAY));
public static PendingIntent newIntent(Context context, int dayId){
Intent intent = new Intent();
intent.setAction("myApp.intent.action.CLOCK");
return PendingIntent.getBroadcast(context, dayId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
Let's say the alarm it setup to repeat every monday at 2pm, the first alarm on monday starts correctly, if I want to test the next week alarm, I go to settings and move the date to the next monday and the time at 1:58pm, to see if it works when it reaches 2pm, however, right after I change the settings, the alarm starts before 2pm.
I'd like to know if there's a more accurate way to test the .setRepeating or if there's something in my configuration that is not working. Thank you!
EDIT:
Thanks to the comments provided, I got it work by adding 7 in the calculation, so I was able to test the alarm by modifying the date and time in the phone in one week:
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calMon.getTimeInMillis(), 7 * 24 * 60 * 60 * 1000, newIntent(this, Calendar.MONDAY));
Thank you!
Related
I am new to android and have problems with setting a repeating alarm. The app is like this: user selects time using a timepicker, and selects several days to set the alarm. I use this code to implement this task:
for(int i=0; i < alarm.getDaysOfTheWeek().size(); i++){
// here is a switch block to assign daysUntilAlarm1
//to a particular day from the list of selected days, like
//daysUntilAlarm1 = Calendar.SUNDAY;
calendar.set(Calendar.DAY_OF_WEEK, daysUntilAlarm1);
calendar.set(Calendar.HOUR_OF_DAY, alarm.getHour()); // hour picked by user
calendar.set(Calendar.MINUTE, alarm.getMinute());
pendingIntent = PendingIntent.getBroadcast(AlarmManagerActivity.this, i, intentToAlarmReceiver, 0);
pendingIntents.add(pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 7 * 24 * 60 * 60 * 1000, pendingIntent);
The problem is when I test alarms, I change the system time to the next alarm day. Everything works fine. But when I change the system time to the past, like last week, the alarm doesn't work. So if user will have to change the time of the device to past, the app will not work. Can anyone help me, please?
I have followed the following link https://developer.android.com/training/scheduling/alarms.html.
RTC examples first one, to set an alarm for a specific time on all days. Even after following the same code, the alarm is not triggered at the time it is suppose to get triggered. Instead the notification gets triggered immediately after setting the time which is done with the help of a time picker. Following is my code snippet,
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, NotificationReceiver.class);
PendingIntent intentalarm = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY,4);
calendar.set(Calendar.MINUTE, 30);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, intentalarm);
Instead of setRepeating I also tried setInexactRepeating but there was no luck.I would also like to add that when I changed the
calendar.getTimeInMillis() and used SystemClock.elapsedRealtime() + 120 * 1000 the alarm triggered exactly after 2 minutes from the time it was set.
But when calendar.getTimeInMillis() is being used the intended working does not happen instead immediate triggering occurs.
Would be indeed very helpful if anyone can help me out find a solution.Only for a note, I could learn if alarm is set before current device time the alarm would be triggered immediately but that is not the case here.
NotificationReceiver.class is working fine as it is generated and appears on the title. But the time it appears is the cause of concern.
You are using both setTimeInMillis and hourofday and minute.
If you want your alarm to be triggered at 4:30 just add hourofday and minute.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,4);
calendar.set(Calendar.MINUTE, 30);`
I could Identify the issue I was facing. I was using a TimePicker with 24 hour format. But I used SimpleDateFormat "hh:mm". What 'h' stands for is as follows "h->Hour in am/pm (1-12) ". So any alarm that I set at AM triggers correctly. When I set a time at PM since I had used 1-12 'h' format, alarm gets set for a AM time and since that time is already passed when compared to the device time the alarm/notification got triggered immediately.The right format to use was "HH:MM" where 'H' stands for "H->Hour in day (0-23)". This resolved my issue.
Checking the TimeinMillis on online epoch time converter helped me identify this issue.Once again thanks for helping me.
I want the AlarmManager to repeat a task at scheduled time (weekly)
I used the following code:
for (Integer day : daysList) {
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_WEEK, PersonalUtils.getDigitalWeek(day));
c.set(Calendar.HOUR_OF_DAY, task.getHour());
c.set(Calendar.MINUTE, task.getMinute());
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
intent.putExtra("id", task.getId());
PendingIntent operation = PendingIntent.getService(
getApplicationContext(), requestCode, intent, 0);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),
AlarmManager.INTERVAL_DAY * 7, operation);
}
However the alarm it triggered prematurely.
For example: Assuming that it is 18:30 Wed now. I setup a task which should be triggered at 17:30 Tue next week but instead the alarm is triggered immediately
Can anybody tell me why?
You are trying to do an inexact alarm, which only allows for a few specific constants, INTERVAL_DAY, INTERVAL_FIFTEEN_MINUTES, etc. See Android Docs for more. Those constants are only supposed to be used for InexactRepeatingAlarms, but I see your doing a RepeatingAlarm.
You have a couple of choices, you can either set the alarm to trigger in exactly 1 week, or you can set it to trigger every day inexactly and only pay attention to it if the alarm occurs during the 7th day. To trigger exactly every 7 days from now, try this:
final long WEEK_IN_MILLIS= 604800000;
alarm.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis()+WEEK_IN_MILLIS,
WEEK_IN_MILLIS, operation);
Note that I set it to first trigger in 1 week, then repeat every week after that. That should work for you.
Im trying to set and run an alarm that run every hour, and is set by a few variables so as it will not run instantly, if the time is greater then the 58th minute
The idea is to set it # X hour and 58 minute, so it will run every hour, at the given minute(58).
Calendar calCurrent = Calendar.getInstance();
int time = 58 ;
Calendar calx = Calendar.getInstance();
calx.set(Calendar.MINUTE, time);
calx.set(Calendar.SECOND, 5);
if (calCurrent.get(Calendar.MINUTE) > time) {
calx.add(Calendar.HOUR, +1);
}
System.out.println("Alarm is set to - " + calx.get(Calendar.HOUR)+":"+
calx.get(Calendar.MINUTE));
alarmSwap = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmSwap.setRepeating(AlarmManager.RTC_WAKEUP,
calx.getTimeInMillis(), 60 * 60 * 1000, pintent);
The code works and runs correctly for the 1st instance, then the alarm will for some reason run # 0 minute the following hour.
Timeline looks like
1:23 - Repeating Alarm Set for 1:58 (1 hour intervals)
1:58 - alarm is triggered
3:00 - alarm is triggered
I have no idea why this alarm is being triggered # :00 for the last alarm. It is not being called from anywhere else.
Any help is greatly appreciated.
All alarms are resetting after the hour clocks over the hour-
Calendar calnew = Calendar.getInstance(); calnew.add(Calendar.SECOND, +5);
alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, calnew.getTimeInMillis(),900000 , pintent);
Timeline-
1:20 triggered
1:35 triggered
1:50 triggered
2:00 triggered
2:15 triggered
2:30 triggered
From android documentation.
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 as described above. Legacy
applications whose targetSdkVersion is earlier than API 19 will
continue to have all of their alarms, including repeating alarms,
treated as exact.
repeating alarms are not exact after API 19. This improves androids performance as android groups alarms which are at close interval and wakes system once and finishes all alarms(from other applications also).
If you really want exact alarms the you will have to set normal one time alarm, and then set alarm again in first alarm's call.
Try this code to repeat alarm every hour
alarmSwap.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
calx.getTimeInMillis(),
AlarmManager.INTERVAL_HOUR, pintent);
Hope this will help.You can ask if you have any further queries.
This worked for me to trigger alarm after every 1hour
alarmmgr = (AlarmManager) getSystemService(ALARM_SERVICE);
long interval = 60 * 60 * 1000; // 1 hour
xassert alarmmgr != null;
alarmmgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
I have read lots of articles, however I did not find/or just missed an answer on my specific quiestion, it is strange cause I think I am trying to implement a common case.
Well, what I whant is to set alarm to fire everyday except the weekends, so from monday-friday at some specific time.
Currently I do next:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
Intent intent = creating an Intent here
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
as far as I understand next code will fire an alarm on the time I have specified and do it every day, because of AlarmManager.INTERVAL_DAY.
I thought about doing next to accomplish my task:
for(int i = 1; i < 6; i++) {
calendar.add(Calendar.DAY_OF_WEEK, i);
}
But I am not sure, about the correctness of this logic.
Can you please
1. correct me, if I missed something
2. suggest some proper solution or just your thoughts to accomplish my task
Update:
Well, I have thought about next:
what if I
calendar.set(Calendar.DAY_OF_WEEK, 1);
and then
am.setRepeating(AlarmManager.RTC_WAKEUP, date.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 5, pendingIntent);
so as a result I will be setting initial day to Monday and repeat it 5 times, so Monday-Friday, no matter what is the current date, when user is setting an alarm, is it correct or I am missing smth?
Btw, how can I update setRepeating to set it to repeat every week, not only one?
Update1:
I guess I understood my error, by using the above code, I will do some strange things, so the init day is Monday, however the repeat interval is once in 5 days, not every day from Monday-Friday.
It seems that the only solution is to set
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
and in the fire handler, which I have specified in the Intent constructor, check the current day and avoid notification if it is Saturday or Sunday.
...
if(intent.getBooleanExtra(INTENT_NOTIFY, false) && !isWeekend())
showNotification();
...
private boolean isWeekend() {
Calendar calendar = Calendar.getInstance();
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
return (dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY);
}
Btw, tell me please, I have set the alarm in repeat mode, for the first time it did fire, then I open emulator settings and manually move date to tomorrow and time back, but no alarm notification is fired, is it smth with emulator or I have some errors in code?