Solved

postDelayed

Posted on 2012-03-18
1
1,218 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

I recently asked a question (http://www.experts-exchange.com/Programming/Smartphones/Android/Q_28684946.html) about Computer Inventory applications for Mobile Devices.  I was specifically interested in an app I could use on my android phone.  The be…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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.
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…

786 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