Solved

postDelayed

Posted on 2012-03-18
1
1,238 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:
Santhana 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Got a software and it removed my google search 6 149
Auto-rotate google map 1 114
Current date-time from Available WiFi connections 10 37
mySMS seems stuck after install 7 30
I tend toward trying the newest hardware and software.  Thiss sometimes works out to my benefit, and sometimes not.  Because I downloaded and installed Android 5.x (http://www.experts-exchange.com/articles/18084/Upgrading-to-Android-5-0-Lollipop.htm…
You should read OS supplied guidelines before developing. I can't stress that enough. The guidelines will help you understand the reasons mobile app developers do what they do.  Apple is very particular when they review appstore submissions.
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.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

829 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