Solved

postDelayed

Posted on 2012-03-18
1
1,248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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
Adups - Android Firmware with Preinstalled Spyware 3 57
spam sms 9 42
moto 4g file transfer 12 76
android: how can i send queued outbox unsent gmail 4 124
A short article about a problem I had getting the GPS LocationListener working.
Here, we have shared an easy step by step tutorial on how to download instagram images and videos on PC, Android Mobile and iOS mobile.
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.

749 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