Testing alarm on setRepeating without waiting 24 hrs - android

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?

Related

Periodic background work using AlarmManager

Is it possible to use AlarmManager to set periodic works? For instance, could I set an alarm for say every 15min for 24 hours? Or put an alarm that schedules another alarm to awaken the app with some delay? Is there any reason for it to no work as expected?
I've tried using periodic workers with WorkManager but the workers consistently failed to wake up after several hours (I've used 15min periods).

Any way to prevent alarm from firing if it did not run because of doze mode?

This Doze mode makes it quite hard for me to implement proper alarms system. Basically I want to allow my user to run a webservice call each 15 or 30 minutes for instance. So let's say the user sets the app to run the alarm each 30 minutes. While the device is normal use, the alarms will fire more or less exact (I'm using setRepeating) but it's good enough for my purpose.
When the device is dozing, as per docs, my repeating alarms do not fire, BUT when the device exits doze mode, all the alarms that did not run, run one after another(or similar). So I end up in the morning with maybe 4-6 alarms fired one after another, all fetching the same data from the webservice. Or same thing when a maintenance window occurs...
Is there a way to tell the doze mode that if my alarm did not fire at required time, to not run it at all? Or if there are multiple alarms that did not fire, only fire the last one?
LE: I had an error in code which ran the alarm too often, that is why I had the impression that postponed alarms will all run one after another...

Why is CountDownTimer not accurate

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

Questions about Alarm Manager behavior and wakelocks

I'm using alarm manager in my service to set non-waking alarms every 15 seconds to execute a certain task. I don't want to wake the phone up as the task isn't time critical, so i'm using the ELAPSED_REALTIME flag to set the alarm. Here's the code:
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), 15 * 1000, intentRecurringChecks);
What I'm noticing in my logs is that the task is getting executed every 15 seconds. Does this mean the phone is staying awake even though it's screen has been turned off for half an hour? Is there a way I can be sure my application's not the one waking up the phone?
I've search about this topic but I can't find a proper answer.
Thanks for your help.
First, you shouldn't use AlarmManager for such timeouts. This is explicitly mentioned in documentation (read the bold part). It's better to use Handler based timers in your case. Here is an example: Repeat a task with a time delay?.
Second, when device is connected via USB, its not going to deep sleep mode. You should disconnect your device wait a minute or two. Attach it back and analyze the logs.

AlarmManager problems when changing the system clock

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

Categories

Resources