I know that this topic is explained fairly well and there are a lot of tutorials. But maybe I'm too new in android to understand what I'm doing wrong.
I need to implement support of set of reminders. And notification should be shown exactly every Monday at 15 pm. I checked a lot of tutorials and similar questions on this site but anyway notification shows, somehow, randomly.
How do I test implementation:
Current time is 14:55
I set reminder on Monday at 15:00
And right after SAVE notification is shown.
Because of (just for example) I set repeat period in 20 sec, this notification is shown again and again with delay of 20 sec. But current time is still between 14:55 and 15:00.
And my task is to run notification at 15:00 or a liitle later. But not before.
set repeating notification
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, NotifyService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, (int) reminder.id, intent, 0);
final Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.DAY_OF_WEEK, 2);
calendar.set(Calendar.HOUR_OF_DAY, reminder.time.hour);
calendar.set(Calendar.MINUTE, reminder.time.minute);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP,
Calendar.getInstance().getTimeInMillis(), 20000, pendingIntent);
BTW
my min SDK version is 7, so I cannot use methods like setExact()
android version on tested device is 4.4.2
Thanks
When you set the alarm you set it with Calendar.getInstance().getTimeInMillis() instead of calendar.getTimeInMillis(). Simple bug ;).
Basically setting the alarm to 'now' every time, ignoring your calendar object.
Related
I want to trigger a notification each day at a specific time choosen by the user, like 6' / 7' / 8'.
For this, I created a WakefulBroadcastReceiver, that pass to an IntentService to create the notification.
And this is how I setup my AlarmsManager. timeInHours is an integer passed as parameter, between 6 and 12 :
Intent i = new Intent(context, StepCountNotifyBroadcast.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
// Get the next day at timeInHours hours'.
Calendar cal = Calendar.getInstance();
cal.setTime(new Date()); // compute start of the day for the timestamp
cal.set(Calendar.HOUR_OF_DAY, timeInHours);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
cal.set(Calendar.AM_PM, Calendar.AM);
if (new Date().getTime() > cal.getTime().getTime())
cal.add(Calendar.DAY_OF_YEAR, 1);
long nextDay = cal.getTime().getTime();
// Setup the alarm.
long timeBetween = AlarmManager.INTERVAL_DAY; // Each day.
AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarms.cancel(pi);
alarms.setRepeating(AlarmManager.RTC_WAKEUP, nextDay, timeBetween, pi);
Date nextDayAsDate = new Date(nextDay);
GLog.d("AlarmUtils", "scheduleNotifiation for next date: " + nextDayAsDate.toString());
It works well 50% of the time, but still do crazy things like... Trigger the notification at 2:00 AM ?!
I know the system will not trigger the notification at the specific time I want and this is ok. But here we talk about approximately 18 hours later.
In logs, it seems that my IntentService code is effectively running in the middle of the night in some cases. So that's not an issue with the notification itself.
I can switch this line :
alarms.setRepeating(AlarmManager.RTC_WAKEUP, nextDay, timeBetween, pi);
to another line of code :
alarms.setExact(AlarmManager.RTC_WAKEUP, nextDay, pi);
But this is not good for the battery life I'm afraid.
Now my questions :
Can somebody tell me a reason about the way Android devices works with AlarmsManager ?
Do you find another solution than will not drain battery life and be sure the notification will be triggered in maximum 2 or 3 hours later?
Do you have any ideas, in this particular case, to debug and do code testing without waiting a day or two to detect issues?
Thanks in advance.
After API Level 23, Doze mode was introduced on the Android System to reduce battery consumption. Go through below link:
How to make Alarm Manager work when Android 6.0 in Doze mode?
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 don't understand what's wrong but my alarm runs every single day even if I hardcode the day number, I have no idea whats going on...
Intent notificationIntent = new Intent(AddTask.this,CustomBroadcastReceiver.class);
notificationIntent.putExtra(Intent.EXTRA_UID,newTaskId);
PendingIntent pendingIntent = PendingIntent.getBroadcast(AddTask.this, newTaskId, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(getApplicationContext().ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.DAY_OF_WEEK,2);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY * 7, pendingIntent);
today is sunday so the day number is 1 and i wrote calendar.set(Calendar.DAY_OF_WEEK,2); and the alarm just got trigered since i used the time and minute of this moment... it doesnot matter if i set the day number 1 2 3 4 5 or 6 or even if i make several alarm managers with same ID and each contains other day it triggers every day
Why is this line necessary?
calendar.setTimeInMillis(System.currentTimeMillis());
Also, the android development documentation recommends using
setInexactRepeating(int type, long triggerAtMillis, long intervalMillis, PendingIntent operation);
EDIT:
I suspect the intent isn't set for creating alarms so it's not creating the alarms correctly.
Maybe this would work:
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
//set repeating alarm here
I hope that answers the question.
So okay i have solved this , for everyone who want to create an alarm application like this > you set the time and hour and the days of week to trigger the alarm, it works like this : if today is the day which you choosed you just set the alarm with this :
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
but IF the day you picked is not the day which is today you have to the the date of the nearest day you picked and then just set the additional hour and minute as above.
this is the only way to make it work
This is a code that i'm using for running a service once a day .
the problem is this , after 24h , the service keeps calling every 10 to 15 min .
Intent myIntent = new Intent(this, MyService.class);
pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.HOUR, 24);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), 86400, pendingIntent);
I think I'm using a bad code .Could you give me a better code for runnig a service once or twice a day
thanks you
The interval parameter to setRepeating represents millis. It should be 86400000 to get it right.
You are now programming an alarm to start first execution 24h past the current time, but with a period of 86400 millis (less than two minutes).
Morale of the story:
Always read the docs before using an API.
Use AlarmManager.INTERVAL_DAY instead of your own magic numbers
I want to set notifications/alarms everyday at specific times. Please note that these times could be different for everyday. For example I have a Database from which I get 5 different entries/times for a day e.g 6am, 1.30pm, 4.30pm, 7.30pm, 10.00 pm. I want to set an alarm for these times. For the next day these times could be different. They could be off by 2-5 minutes or more.
Basically I cannot set a recurring alarm for the same time everyday. I need to check the entry in my Database to know what time I should schedule it.
What is a good and efficient way of doing this. I looked at some stack overflow questions for setting multiple alarms. But here how do I do it? Should I just read the whole weeks database entries i.e 5 times/day for 7 days..And set around 35 alarms together? Or should I just set one alarm at a time. And when that alarm is fired just read the next entry from the Database and schedule an alarm for that time?
Write a service which will pick up the 5 values from the database everyday at a particular time.
Then add multiple entries in the AlarmManger with different unique ID.
Adding alarm at particular time:
Calendar calendar = Calendar.getInstance();
//9 AM
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pi = PendingIntent.getService(context, 0, new Intent(context, YourClass.class), PendingIntent.FLAG_UPDATE_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
Setting Multiple alarm:
AlarmManager[] alarmManager=new AlarmManager[24];
intentArray = new ArrayList<PendingIntent>();
for(f=0;f<arr2.length;f++){
Intent intent = new Intent(AlarmR.this, Riciving.class);
pi=PendingIntent.getBroadcast(AlarmR.this, f,intent, 0);
alarmManager[f] = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager[f].set(AlarmManager.RTC_WAKEUP,arr2[f] ,pi);
intentArray.add(pi);
}
Hope this should work.