I have an app that writes some data to a file every second.
I used a Timer that and schedule it at a fixed interval of 1000
for the most part it works fine, but sometimes i notice there are seconds, or sometime even minutes where there is no data in the log file.
Is there any likely suspects to this problem?
Timer runs in background thread(s) that are not reliable. From the docs:
Each timer has one thread on which tasks are executed sequentially. When this thread is busy running a task, runnable tasks may be subject to delays.
LINK: http://developer.android.com/reference/java/util/Timer.html
If you have a time sensitive task, then you need to check SystemClock like here:
How to measure elapsed time
Related
I am trying to schedule a job service when i receive BOOT_COMPLETED. Job is getting scheduled immediately and again getting scheduled periodically.
Here depending on some local criteria some time i need to delay the scheduling of job so i can call scheduleJob of scheduler after this delay.
My app does not have any UI and always runs in background , to provide delay i tried timer task , handler etc but these only worked in foreground conditions.
As my app is running in background these did not helped me .
I again tried starting intent service as foreground service and in onHandleIntent trying to sleep the thread for specified time.Once sleep time is over i am scheduling the job and stopForgroundService . Max delay i need to handle is 24 hours.
Is this correct approach to provide delay of max 24 hours and hold it using foreground service or is there any other way i can achieve this.
IntentService, TimerTask
Recently, I have been working on scheduling repeated tasks in background threads in Android application. I started with AlarmManager but due to its inaccurate triggers at specified intervals, I decided to use other JAVA and Android APIs.
I started with Timer and then ScheduledExecutorService and lastly ScheduledThreadPoolExecutor.
The problem with Timer is that it does not always run all the background tasks at right intervals. I had used it to run 3 background repeating tasks out of which only 2 were getting triggered. The third task was triggered after a couple of hours and to catch up with all the previous misses, it was triggered within 10ms (although the interval was 5 mins)
ScheduledThreadPoolExecutor is more accurate in triggering the repeated background tasks. But the problem with it is that it does not trigger the background tasks when the device is in sleep mode (screen off). The same is the behavior with Handler.postDelayed() which pauses the execution of tasks when the device is in sleep mode.
Now, I have reached a dead end. I am looking for a solution that could help me run my background tasks concurrently and without any interruption due to device going to sleep mode.
Please advice.
I am making some sort of timer, which uses a Service to define a Runnable which is being ran after the user defined amount of minutes. The problem is that when the user defined amount of minutes is about (or higher than) 120 minutes, it seems the system forgets about the Runnable, and doesn't perform the tasks in there.
My question is: Is there a better way to schedule tasks to be ran after a specified amount of time?
Thanks in advance!
use alarm manager to trigger the event after some time refer here http://www.learn-android-easily.com/2013/06/scheduling-task-using-alarm-manager.html
I have gone through various discussion threads and tried multiple iterations to have my application scheduled to repeatedly run at fixed intervals. I have been successful too, but starting the thread to get understanding how things work.
I had tried the implementation with ScheduledExecutorService as well as TimerTask and both did not trigger my task at right intervals. But found success with AlarmManager.
Please lead me in right direction to understand why the timing did not work well with ScheduledExecutorService and TimerTask.
The app is required to perform a task on a regular interval. Say, my task takes around 5 minutes to complete, I would like to keep it scheduled so that it runs once in every 30 minutes. When I built the application with ScheduledExecutorService or TimerTask, I found that it does not trigger the task at 30 minutes, but it triggered at 1 hour or even more. So far, with Alarm Manager, it seems to be working fine getting triggered at around 30 minutes(slight variation is still found, but better than the other two).
When I built the application with ScheduledExecutorService or TimerTask, I found that it does not trigger the task at 30 minutes, but it triggered at 1 hour or even more.
More importantly, it will not trigger at all once your process is terminated, which can happen at any point and, from the user's standpoint, should happen quickly, to free up RAM for other apps.
Your trigger delays are probably due to the device falling asleep. Your solution would not only tie up the user's RAM, but also would require you to prevent the device from falling asleep, which is horrible for battery life.
AlarmManager is a far more appropriate solution, as it does not require you to keep a service running or otherwise have a process around. Just be sure to use WakefulBroadcastReceiver as the way you respond to your _WAKEUP event, as the device will want to fall back asleep.
My Application needs to have one (async) task which is always a network operation and the first time a DB query as well.
It needs to be executed with a certain interval amount of time which is not fixed (could be 1 minute or 1 hour or anything in between) and also when the application is not active.
What is the best and the simplest solution in such case between Asynctask/Thread/Service/Alarm/anything not mentioned.
For that you need a service, not an AsyncTask:
http://www.vogella.com/tutorials/AndroidServices/article.html
Services persist even after activities are dismissed. Be careful though, draining the battery will anger your users. Also, services can be destroyed if memory runs low, so be careful.
The scheduling of a task to run in the future should use AlarmManager. The execution of the operation should run in a service since you specify that sometimes it will occur when the app is not active.
You can also use a TimerTask but it's a little more drain on battery and not as precise as AlarmManager. You can also use a handler to postAtTime
Here is a good reference:
difference between timer and alarmmanager
and another one:
What is better in Android? Timer or Alarm?
It really depends on the frequency and use-case. I've used all of these - I view TimerTasks as "keep checking frequently for short time" vs. handlers for "check again in a while" vs. AlarmManager for "I want to keep checking every hour for a long while"