I'm using an AlarmManager with an interval of 5 minutes, and I have made some tests for a few days and it seems that the interval of the AlarmManager isn't constant all the time, but it changes some times. This is causing my app not to work as it sould.
Why is this happening? Is there a solution?
The code I am using is:
iHeartBeatService = new Intent(SpyMe.context, HeartBeat.class);
piHeartBeatService = PendingIntent.getService(SpyMe.context, 101010, iHeartBeatService, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 5 * 60 * 1000, piHeartBeatService);
on a Samsung Galaxy 3 I5800 with Android 2.2
You need to post your code for us to help you. I'm going to shoot in the dark here and guess that you're not using RTC_WAKEUP for your alarm.
alarm.set(AlarmManager.RTC_WAKEUP, calander.getTimeInMillis(), pendingIntent);
Related
I need to run a task to collect some data 4 times per hour. I guess best way to do it is using AlarmManager. I managed to get following code working and it runs for every 10 sec.
AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
int interval = 10000;
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
But I want my task to run 4 times per hour. Exactly at XX.00, XX.15, XX.30, XX.45. This is to generate some data graphs.
Can somebody tell me how to archive this? Should I use Calendar class? I could not find any clear method.
Thanks!
Try this:
put interval=15*60*1000 and instead of System.currentTimeMillis() put the milliseconds of XX:00.If you want this time from user then You can use using TimePicker dialog to do so.
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.
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
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 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.