I have 2 CountDownTimer objects in my app, that count the time to next event constantly as the app is running.
So today I debugged it, and found out that the timer is not accurate at all, I have given it an interval of 1 hour, starting at 9:00 am, and now 14:04 was when the next event fired. whole 4 mins late, and it is getting more and more with each event.
So should I use the CountDownTimer for such task, or I should just make an if check in it's onTick() method, where every second I will check for fixed time passed since the previous event, then reset the time for after 1 hour?
I can't do these tasks with AlarmManager, since all these things are happening in foreground service , and as much as I know, Alarms will be killed if the users swipes the app away from the running apps menu
Related
I am trying to use the GCMTaskManager to schedule a periodic task that runs approximately every 30 minutes. When it runs, it does an "update" on some internal data in the APP.
I have this preliminarily running and can see, using a LOG command in the OnRunTask, that the task fires as desired.I have it operating (at least according to a LOG I am populating when the OnRunTask fires.
I have two issues I can't determine if it can be done....
Is there a way to see how much time is "left" before the next OnRunTask fires... or... approximately what "time" it should run next? Example, if there is 30 minutes for the PeriodTask... and 18 minutes have lapsed, can we programmatically see that 12 minutes remain?
I have an internal "button" to manual fire the APPS update. If the user hits this, I want to "restart" the PeriodicTask timer from that point. That is, if the 30 minute OnRunTask is suppose to fire in approximately 12 minutes, and the user hits the UPDATE manually, I want the OnRunTask to now restart from for 30 minutes from the button press. Can this be done?
Thanks
Pete
For 1) GCMNetworkManager does not provide an interface for keeping track of the time elapsed/remaining but you can do so yourself by maintaining a variable storing the last updated timestamp for your data.
2) From the docs
When Google Play Services or the client app is updated, all scheduled tasks are removed. GcmNetworkManager invokes the client app’s onInitializeTasks(). Override this function to reschedule necessary tasks.
I've setup two service alarms to use setRepeating at different intervals. Alarm A activates and repeats at 9am. Alarm B stops Alarm A service and repeats every 12pm. This seemed to work (with patience) for three days in a row starting and stopping the service, but this morning the test didn't work.
Without a debugger I'm not sure how I can see the logs to determine what went wrong. I'd also like to be able to test the alarms without waiting for the 24 hour intervals. Is this possible? And is anyone familiar with services not starting and stopping consistently?
is there any way to stop an alarm at a given time, for example, i started an alarm on 7:45am and it repeats every 5 minutes and then, i wanted to stop that alarm when the time is already 8:00am?
How do i go about this algorithm since alarm manager's cancel() method only accepts an action (PendingIntent)
and Secondly, regarding services, i have an idea to put a checker or an if statement on service to check if its already 8:00a, but im not sure if service always run in the background and if so, does that mean to say that it always checks the time if its 8am? given that meaning of services that once it started i won't stop unless explicitly told to do so.
any of you guys know any way to do this please do share, im kinda confused right now
you will have to start the service when alarm start first time and this service will ring the alarm at 5 min and get the device time and compare it with the time to stop the time
at that (means at 8 AM )stop your service
Why dont you start whatever you need to do at 7:45am with AlarmManager#setRepeating and set another alarm with AlarmManager#set to stop the previous?
AlarmManager#set will be fired off just once, so you can use it cancel the repeating alarm
Edit:
In the implementation of AlarmManager#set, you would retrieve the repeating alarm(AlarmManager#setRepeating) and cancel() it
I have a service that updates some data every minute. When I change the system clock of the phone or the emulator, the timer executes immediately n times with no delay between them.
Let's suppose that it is 10:00 pm. If I change the time to 11:00pm, the timer runs 60 times one by one with no delay between each run. My service generates HTTP requests so it'll trigger 60 request one by one for 4-5 secs.
What is wrong? I have got the same issue with the AlarmManager too. How can I prevent this behavior?
There is an issue for this
http://code.google.com/p/android/issues/detail?id=17486
if u change system time before u have to cancel PendingIntent and restart it
I have few questions about getting current location of the device.
I believe only way to do that is using requestLocationUpdates(....).
if I subscribe to requestLocationUpdates... with duration set to 1 hour and after 1 hour if my phone is sleeping (or during that 1 hour I rebooted my phone), does the listener still work?
A. If yes - After 1 hour, when I get update about location, I want to change duration to 2 hours. Is there a way to do that? If not, can I call removeUpdates, and immediately call requestLocationUpdates with duration as 2 hours?
B. If No - I was planning to set an alarm to go off after 1 hour, and call removeUpdates & requestLocationUpdates in the alarm receiver. Is that right strategy.
I'm not 100% sure but If you reboot your phone service isn't started again, also as is written in a manual:
minTime - the minimum time interval for notifications, in milliseconds. This field is only used as a hint to conserve power, and actual time between location updates may be greater or lesser than this value.
This is only a hint for update time but it's not reliable.
Your practice should be to make a Service which should use Handler and postDelayed to set timed action within a runnable. I hope I helped.