I am a bit confused about Timer and AlarmManager used in Android.
What are the main differences between them?
They are both scheduling a task to run at every A seconds. And what is the main scenario that they are preferred to be used?
For example, for X situation, use Timer but on the other hand, for Y situation, use AlarmManager.
A Timer will start a thread that will keep track of when to start your code. If the device goes asleep, so will the timer thread and your code won't be executed on time. AlarmManager's alarms, on the other hand, are kernel-level. Depending on how you register them, you can request to wake up the device, or execute the next time something wakes up the device. Alarm's are generally preferable and use less resources.
Timer starts a service it executes code very frequently even thought it wasn't actually doing anything.
Alarmmanager on the other hand will start a Service that runs in the background always, this is what you want to use to schedule your code to run when your app isn't open.
Related
I'm looking for Android specific implementation detail of ScheduledExecutorService.schedule when device is in deep sleep. I understand that schedule will not guarantee exact timing but simply execute "after" the delay. What I am not clear on is how sleep (and deep sleep?) is accounted in the timing. To be specific scenario:
Schedule a task an hour later
5mins in, the phone goes to sleep for 30mins
When do I expect the task to get scheduled?
Also if the phone wake up long after the scheduled time, is the task then scheduled for execution immediately?
As far as this post says Difference between AlarmManager and ScheduledExecutorService, it won't work in deep sleep mode. And I think it should be pretty simple to test it on an android device if you try to run a task after some arbitrary time and just wait for device to go in deep sleep mode.
I use the Java's Timer to schedule a task to run after some interval of time.
myTimer.schedule(myTask, delayTime);
At any point in time, is it possible to check if there is any task scheduled to be run (but has not run yet)? If so, how would I do that?
If not, what is the alternative(s) to Timer do I have?
You can (and should) use ScheduledExecutorService instead of Timer.
It handles thread crashes in a robust manner and has more flexible API
You can just add a boolean field to myTask's class, which will be set true at first execute.
Keep it simple.
I'm basically scheduling a task to run every ten seconds, but it seems like it runs every 8 or 9. I use the function schedule (task, 0 , 10000);
I'd say it's pretty accurate. In my app(Tap Counter) I use a timer to start a task, and a seperate timing system to display a countdown, and they sync perfectly.
Feel free to check out my app =)
try the Alarm Service to schedule a task.
Hope this links will be helpful http://android-er.blogspot.com/2010/10/simple-example-of-alarm-service-using.html
How should I implement a function like that on my Swing editor?
I was thinking of a thread started on a releaseKey event. This thread should have a timer of a second. Every time I have the releaseKey I either start the thread or just reset the timer if it is already running.
I'm not convinced though. It seems like too heavy on the UI.
How should I do it?
A Timer starting/stopping every second is not a big weight on the UI at all. The "building" is what is going to possibly take some time. I think looking for a pause in keystokes is a fine solution.
I am working on a Java program and using Timer objects to run tasks every few minutes or hours. This works fine in normal operations, but I am running into a problem with "Sleep mode" on Mac (maybe on other OSes, but I haven't tried yet).
Consider this code sample:
//Setup the timer to fire the ping worker (every 3 minutes)
_PingTimer.scheduleAtFixedRate(new TimerTask(){
public void run(){
Program.PingThread = new PingWorker(Settings.Username, Settings.UserHash, true, true);
Program.PingThread.CheckOpenPort = true;
Program.SwingExecutor.execute(Program.PingThread);
}
}, 0, 180000);
In normal operation this would fire every 3 minutes with enough accuracy (I'm not concerned about the exact second or anything). The problem with this is after sleeping the computer for a few hours or so it seems to just BLAST the system with backlogged timer requests.
It seems to be running all of the missed timer hits during sleep at once trying to make up for lost time.
Is there a way i can prevent this? I tried using synchronized and some other thread techniques, but this only ensures that they aren't all running at the same time. They still continue to run one after another until the backlog is passed.
Thanks for any help you can provide!
Have you looked at the API? It clearly states the following:
In fixed-rate execution, each
execution is scheduled relative to the
scheduled execution time of the
initial execution. If an execution is
delayed for any reason (such as
garbage collection or other background
activity), two or more executions will
occur in rapid succession to "catch
up." In the long run, the frequency of
execution will be exactly the
reciprocal of the specified period
(assuming the system clock underlying
Object.wait(long) is accurate).
This is one reason why you should consider using a ScheduledExecutorService. This link may also prove useful.
Use schedule instead of scheduleAtFixedRate.