anb123
anb123

Reputation: 33

Timers- Async Task or Threads

I wanted to know what is the best way to disable a button for a certain amount of time. When the user uses other buttons to go to other pages, the timer will not be reset and the button will still be disabled.

There are many different ways i have read about async tasks, threads. However i am very unsure which is the best way to do this.

I want the timer to keep running while user selects other pages in the app. if the person tries to press the timer before the timer is up it will not work.

Many thanks,

Upvotes: 1

Views: 4068

Answers (4)

ucsunil
ucsunil

Reputation: 7494

I can see (from your comment to one of the answers) that you are aware that AsyncTask should not be run for a long time. The reason is because AsyncTasks are tied to the activity (since it is generally an inner class of the activity) and holds a reference to it. Because of this Android cannot GC the Activity while the AsyncTask is running which is kind of one of the core concepts of Android. This will lead to memory leaks in time. There are workarounds to avoid this but Android specifically introduces the concept of services for this and I'll explain this shortly.

Using a thread is also not a good idea as you generally end up creating threads inside the class where you need them as a result of which it holds a reference to that class and cannot be GC'd. Threads hold an advantage when certain long running tasks of the activity are going on and the Activity is necessary to be in the foreground. In your specific case, since you will be opening several other pages, Android would want to GC the original activity - thereby negating the advantage of threads.

Services are designed to be long running processes in the background and represent the server in a client-server architecture. The service can monitor the state of an object until the service is killed which happens only under low memory conditions. So even if the activity (or fragment) holding the button is GC'd by Android, you can design the service to remember the time left only after which even if a new instance of the original activity (or fragment) is instantiated, the button will be enabled.

Upvotes: 1

Kevin Krumwiede
Kevin Krumwiede

Reputation: 10288

For simple changes to the UI, use a Handler. Disable the button and post a delayed Runnable to the handler to re-enable it. AsyncTask would be overkill, and Timer would be inappropriate since you should only touch the UI from the UI thread.

You'll need to cancel the Runnable task when the user leaves the activity. To make it appear that the timer is still running when the user is in another activity, save the essential state (i.e., the timer duration and start time) so the task can be posted again (or the UI updated to show that it's finished) when the activity resumes. See the docs on recreating activities.

Edit: this answer to a related question gives a bit more detail.

Upvotes: 0

beyondtheteal
beyondtheteal

Reputation: 858

If you are a beginner at threading, Async tasks make it super easy. It is basically a wrapper for a timer that allows you to do "pre" and "post" and "during" activities. Once you are comfortable with the concept and are ready to get rid of the 'fluff' code, move to using Timers.

Upvotes: 0

Ollie Strevel
Ollie Strevel

Reputation: 871

To schedule a task for a time i use Timer class, with the method schedule

new Timer().schedule(new TimerTask()
{
    @Override
    public void run()
    {
            // your code here
    }
}, 2000 );

After 2000 miliseconds (2 seconds) the method run, will be executed. I hope this help =)

Upvotes: 2

Related Questions