Solved

postDelayed

Posted on 2012-03-18
1
1,188 Views
Last Modified: 2012-06-27
I have a couple questions about postDelayed, see context below.

1. What is postDelayed?
2. What is it used for?
3. When should it be used? In other wods, in what circumstances should it be used?
4. What is "new Runnable()"?
5. Don't you have to declare a Runnable variable first? e.x. Runnable r = new Runnable()
6. What is public void run() if it never gets called. In other words, what's the point of creating a "run" method if it never gets called explicitly. Does it get called implicitly somewhere?

public void onClick(View v)
{
    ToggleButton.postDelayed(new Runnable()
       {
           public void run()
           {
              // do something
           }
        }
}
0
Comment
Question by:InfoTechEE
1 Comment
 
LVL 8

Accepted Solution

by:
santhanasamy earned 500 total points
ID: 37736364
1. What is postDelayed? 2. What is it used for?
From the android documentation Threads
When an application is launched, the system creates a thread of execution for the application, called "main." This thread is very important because it is in charge of dispatching events to the appropriate user interface widgets, including drawing events. It is also the thread in which your application interacts with components from the Android UI toolkit (components from the android.widget and android.view packages). As such, the main thread is also sometimes called the UI thread.
The system does not create a separate thread for each instance of a component. All components that run in the same process are instantiated in the UI thread, and system calls to each component are dispatched from that thread. Consequently, methods that respond to system callbacks (such as onKeyDown() to report user actions or a lifecycle callback method) always run in the UI thread of the process.
For instance, when the user touches a button on the screen, your app's UI thread dispatches the touch event to the widget, which in turn sets its pressed state and posts an invalidate request to the event queue. The UI thread dequeues the request and notifies the widget that it should redraw itself.

PostDelay will put a task/Runnable into the UI Thread with the given delay. In android all the UI related operations will be executed in the UI thread.
By defalult this UI thread will have one handler let say UI handler.
ToggleButton.postDelayed(new Runnable()...,1000);  Here we are passing a task/Runnable into the  UI thread with 1 sec delay

For more information read this docs,,
http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html
http://developer.android.com/reference/android/os/Handler.html

3. When should it be used? In other wods, in what circumstances should it be used?In some situation while executing  Non_UI  thread we want to do some operation in the UI  thread / We want to update UI from Non_UI thread.
In some situations Non_UI thread may have the references of View. But if we try to change any UI state/UI related operation  it will throw the illegal access exception
(Some thig like this :Accessing UI thread from NonUI.....). That kind of situations we will use this postDelay()/post() to interact with UI thread..

4. What is "new Runnable()"?It is a type of Inner class in java called Anonymus inner. Here we are instantiating a anonymus class   that has implemented Runnable interface.
Basically we are creating a Runnable instance.
5. Don't you have to declare a Runnable variable first? e.x. Runnable r = new Runnable()
Yes we can
ToggleButton.postDelayed(r);

6. What is public void run() if it never gets called. In other words, what's the point of creating a "run" method if it never gets called explicitly. Does it get called implicitly somewhere?
Please go through the basic java Threading....
http://docs.oracle.com/javase/tutorial/essential/concurrency/
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Working settings for French ISP Orange "Prêt à Surfer" SIM cards for data connections only. Can't be found anywhere else !
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now