AlarmManager problems when changing the system clock - android

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

Related

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

Testing alarm on setRepeating without waiting 24 hrs

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?

Count-down timer/alarm API for android

I'm learning Android and want to write some kind of count-down timer application, so I get a ring-tone after certain minutes are elapsed. The timer also should work if the user has closed the application or switched to another one. If my application is running, it should be able to display the remaining time.
I've tried with CountDownTimer, but this seems only to work when the phone is activated, but not like the alarms which could ring you up at the morning. What other similar API alternatives are there to activate the device if the time is elapsed?
You can use AlarmManager for this purpose.
count-down and alarm are two very different things (even thou both count time).
Count-down you probably want to run a service and put a notification with the flag ongoing = true updating the value of the time.
Alarm you want to use the AlarmManager (as pointed out by #PgmFreek) that you can schedule a specific time that the system will call an Intent for you.

Need to implement a timeout feature when application goes to background

I need to create a timeout feature when my app goes to the background for 5 minutes(anything that fires up onPause() except when activity is finishing). If the user goes back to the application then the timer should be cancelled.
Also, I need the timer to be not dependent on the time set in the phone meaning when the app goes to the background and then the user changes the time the application will still timeout within 5 minutes.
Checking out the documentation of the AlarmManager it states the following:
Note: The Alarm Manager is intended for cases where you want to have your application code
run at a specific time, even if your application is not currently running. For normal
timing operations (ticks, timeouts, etc) it is easier and much more efficient to use
Handler.
Alternatively, you could try setting up bound service - these can operate in background even if user toggles frontmost application. you can communicate with the service using Handler, just as you would do with threads.
the easiest would be to use sendEmptyMessageDelayed of predefined type and use call to removeMessages() once your app is back on top.
You should take a look at the AlarmManager. You can easily program your app to run at a specific time or every 5 minutes with a PendingIntent. You can cancel it with the same PendingIntent.
I don't know about the time dependency but it's easy to test.
in onPause() set an alarm using AlarmManager, like this:
AlarmManager alarmManager = getSystemService(Context.ALARM_SERVICE);
// Alarm time is elapsed time since boot (including sleep) plus 5 minutes
long alarmTime = SystemClock.elapsedRealtime() + 5 * 60 * 1000;
alarmManager.set(AlarmManager.ELAPSED_REALTIME, alarmTime, pendingIntent);
For the pendingIntent() you can use either a broadcast Intent or you can have it start one of your activities. Depends what you want to do when the alarm goes off.
NOTE: Using AlarmManager.ELAPSED_REALTIME ensures that changes the user makes to his date/time settings won't have any effect on your 5 minute timeout.
Make sure to cancel the alarm when your app resumes (in onResume()) and when it finishes (in onDestroy())
I wonder if the easiest solution wouldn't be to just set a value in sharedPreferences when your activity pauses and test this value again onResume. To avoid the value being corrupted by the user changing the clock you could employ SystemClock.elapsedRealtime() or SystemClock.uptimeMillis()

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.

Categories

Resources