I am new to Android. I want to know what the Looper
class does and also how to use it. I have read the Android Looper class documentation but I am unable to completely understand it.
I have seen it in a lot of places but unable to understand its purpose. Can anyone help me by defining the purpose of Looper
and also by giving a simple example if possible?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
Looper allows tasks to be executed sequentially on a single thread. And handler defines those tasks that we need to be executed. It is a typical scenario that I am trying to illustrate in this example:
Now we can use the handler in some other threads(say ui thread) to post the task on Looper to execute.
On UI thread we have an implicit Looper that allow us to handle the messages on ui thread.
What is Looper?
Looper is a class which is used to execute the Messages(Runnables) in a queue. Normal threads have no such queue, e.g. simple thread does not have any queue. It executes once and after method execution finishes, the thread will not run another Message(Runnable).
Where we can use Looper class?
If someone wants to execute multiple messages(Runnables) then he should use the Looper class which is responsible for creating a queue in the thread. For example, while writing an application that downloads files from the internet, we can use Looper class to put files to be downloaded in the queue.
How it works?
There is
prepare()
method to prepare the Looper. Then you can useloop()
method to create a message loop in the current thread and now your Looper is ready to execute the requests in the queue until you quit the loop.Here is the code by which you can prepare the Looper.
Definition of Looper & Handler:
Looper is a class that turns a thread into a Pipeline Thread and Handler gives you a mechanism to push tasks into it from any other threads.
Details:
So a PipeLine Thread is a thread which can accept more tasks from other threads through a Handler.
The Looper is named so because it implements the loop – takes the next task, executes it, then takes the next one and so on. The Handler is called a handler because it is used to handle or accept that next task each time from any other thread and pass to Looper (Thread or PipeLine Thread).
Example:
A Looper and Handler or PipeLine Thread's very perfect example is to download more than one images or upload them to a server (Http) one by one in a single thread instead of starting a new Thread for each network call in the background.
Read more here about Looper and Handler and the definition of Pipeline Thread:
Android Guts: Intro to Loopers and Handlers
FROM DOCS
Looper
Looper
Class used to run a message loop for athread
. Threads by default do not have a message loop associated with them; to create one, callprepare()
in the thread that is to run the loop, and thenloop()
to have it process messages until the loop is stopped.Looper
is a message handling loop:MessageQueue
, which contains a list messages. An important character of Looper is that it's associated with the thread within which the Looper is created.Looper
is named so because it implements the loop – takes the next task, executes it, then takes the next one and so on. TheHandler
is called a handler because someone could not invent a better nameLooper
is a Java class within the Android user interface that together with the Handler class to process UI events such as button clicks, screen redraws and orientation switches.A thread gets a
Looper
andMessageQueue
by callingLooper.prepare()
after its running.Looper.prepare()
identifies the calling thread, creates a Looper andMessageQueue
object and associate the threadSAMPLE CODE
For more information check below post
A Looper has a
synchronized
MessageQueue
that's used to process Messages placed on the queue.It implements a
Thread
Specific Storage Pattern.Only one
Looper
perThread
. Key methods includeprepare()
,loop()
andquit()
.prepare()
initializes the currentThread
as aLooper
.prepare()
isstatic
method that uses theThreadLocal
class as shown below.prepare()
must be called explicitly before running the event loop.loop()
runs the event loop which waits for Messages to arrive on a specific Thread's messagequeue. Once the next Message is received,theloop()
method dispatches the Message to its target handlerquit()
shuts down the event loop. It doesn't terminate the loop,but instead it enqueues a special messageLooper
can be programmed in aThread
via several stepsExtend
Thread
Call
Looper.prepare()
to initialize Thread as aLooper
Create one or more
Handler
(s) to process the incoming messagesLooper.loop()
to process messages until the loop is told toquit()
.