我是安卓系统的新手。我想知道Looper类做什么,以及如何使用它。我已经阅读了Android Looper类文档,但我无法完全理解它。 我在很多地方见过它,但无法理解它的目的。谁能帮我定义一下Looper的目的,如果可能的话,也给我一个简单的例子?
当前回答
Android Looper是一个包装器,用于将messagqueuue附加到线程,并管理队列处理。它在Android文档中看起来非常神秘,很多时候我们可能会面临与Looper相关的UI访问问题。如果我们不了解基础知识,就很难处理。
这是一篇文章,解释了Looper的生命周期,如何使用它,以及在Handler中使用Looper
循环器=线程+消息队列
其他回答
Looper & Handler的最简单定义:
Looper是一个将线程转换为管道线程的类,Handler为您提供了一种机制,可以将任务从任何其他线程推入该管道。
一般措词的详细说明:
因此,流水线线程是一个可以通过处理程序从其他线程接受更多任务的线程。
Looper之所以这样命名,是因为它实现了循环——接受下一个任务,执行它,然后再接受下一个任务,以此类推。Handler之所以被称为Handler,是因为它用于处理或接受每次来自任何其他线程的下一个任务,并传递给Looper(线程或管道线程)。
例子:
循环程序和处理程序或管道线程的非常完美的例子是下载多个图像或在单个线程中逐个上传到服务器(Http),而不是在后台为每个网络调用启动一个新线程。
在这里阅读更多关于Looper和Handler以及Pipeline Thread的定义:
Android内脏:环形器和处理程序的介绍
java线程的生命周期在run()方法完成后结束。同一线程不能再次启动。
Looper将普通线程转换为消息循环。Looper的主要方法有:
void prepare ()
将当前线程初始化为循环程序。这使您有机会创建处理程序,然后在实际开始循环之前引用此循环程序。请确保在调用此方法后调用loop(),并通过调用quit()结束它。
void loop ()
在此线程中运行消息队列。确保调用quit()来结束循环。
void quit()
退出循环器。 导致loop()方法终止,而不处理消息队列中的任何其他消息。
Janishar的这篇mindorks文章很好地解释了核心概念。
循环程序与线程相关联。如果你在UI线程上需要Looper, loop . getmainlooper()将返回相关的线程。
您需要将Looper与Handler相关联。
Looper, Handler和HandlerThread是Android解决异步编程问题的方法。
有了Handler之后,就可以调用下面的api了。
post (Runnable r)
导致Runnable r被添加到消息队列。可运行对象将在附加此处理程序的线程上运行。
boolean sendMessage (Message msg)
在当前时间之前的所有挂起消息之后,将消息压入消息队列的末尾。它将在附加到此处理程序的线程中的handleMessage(Message)中接收。
HandlerThread是一个方便的类,用于启动具有循环程序的新线程。循环程序可以用来创建处理程序类
在某些情况下,你不能在UI线程上运行可运行任务。 例如,网络操作:在套接字上发送消息,打开URL并通过读取InputStream获取内容
在这些情况下,HandlerThread很有用。你可以从HandlerThread获取Looper对象,并在HandlerThread上创建Handler而不是主线程。
HandlerThread代码是这样的:
@Override
public void run() {
mTid = Process.myTid();
Looper.prepare();
synchronized (this) {
mLooper = Looper.myLooper();
notifyAll();
}
Process.setThreadPriority(mPriority);
onLooperPrepared();
Looper.loop();
mTid = -1;
}
参考下面的示例代码:
Android:在线程中吐司
I will try to explain the purpose of looper class as simple as possible. With a normal thread of Java when the run method completes the execution we say the thread has done it's job and thread lives no longer after that. what if we want to execute more tasks throughout our program with that same thread which is not living anymore? Oh there is a problem now right? Yes because we want to execute more tasks but the thread in not alive anymore. It is where the Looper comes in to rescue us. Looper as the name suggests loops. Looper is nothing more than an infinite loop inside your thread. So, it keeps the thread alive for an infinite time until we explicitly calls quit() method. Calling quit() method on the infinitely alive thread will make the condition false in the infinite loop inside the thread thus, infinite loop will exit. so, the thread will die or will no longer be alive. And it's critical to call the quit() method on our Thread to which looper is attached otherwise they will be there in your system just like Zombies. So, for example if we want to create a background thread to do some multiple tasks over it. we will create a simple Java's thread and will use Looper class to prepare a looper and attach the prepared looper with that thread so that our thread can live as longer as we want them because we can always call quit() anytime whenever we want to terminate our thread. So our the looper will keep our thread alive thus we will be able to execute multiple tasks with the same thread and when we are done we will call quit() to terminate the thread. What if we want our Main thread or UI thread to display the results computed by the background thread or non-UI thread on some UI elements? for that purpose there comes in the concept of Handlers; via handlers we can do inter-process communication or say via handlers two threads can communicate with each other. So, the main thread will have an associated Handler and Background thread will communicate with Main Thread via that handler to get the task done of displaying the results computed by it on some UI elements on Main thread. I know I am explaining only theory here but try to understand the concept because understanding the concept in depth is very important. And I am posting a link below which will take you to a small video series about Looper, Handler and HandlerThread and I will highly recommend watching it and all these concepts will get cleared with examples there.
https://www.youtube.com/watch?v=rfLMwbOKLRk&list=PL6nth5sRD25hVezlyqlBO9dafKMc5fAU2&index=1
循环器有一个同步的messagqueuue,用于处理放置在队列上的消息。
它实现了线程特定的存储模式。
每个线程只有一个Looper。关键方法包括prepare()、loop()和quit()。
prepare()将当前线程初始化为循环程序。prepare()是使用ThreadLocal类的静态方法,如下所示。
public static void prepare(){
...
sThreadLocal.set
(new Looper());
}
在运行事件循环之前,必须显式地调用Prepare()。 loop()运行事件循环,等待消息到达特定线程的消息队列。一旦接收到下一个Message, loop()方法将该Message分派给它的目标处理程序 Quit()关闭事件循环。它不会终止循环,而是将一条特殊消息编入队列
Looper可以通过几个步骤在线程中编程
扩展的线程 调用loop .prepare()将Thread初始化为Looper 创建一个或多个Handler来处理传入消息 调用loop .loop()来处理消息,直到循环被告知quit()。
什么是环形使者?
Looper是一个用于执行队列中的消息(可运行对象)的类。普通线程没有这样的队列,例如简单线程没有任何队列。它执行一次,方法执行完成后,线程将不再运行另一个Message(Runnable)。
我们可以在哪里使用Looper类?
如果有人想要执行多个消息(Runnables),那么他应该使用负责在线程中创建队列的Looper类。 例如,在编写从internet下载文件的应用程序时,我们可以使用Looper类将要下载的文件放入队列中。
它是如何工作的?
有prepare()方法来准备Looper。然后,您可以使用loop()方法在当前线程中创建一个消息循环,现在您的Looper已经准备好在队列中执行请求,直到退出循环。
下面是您可以准备Looper的代码。
class LooperThread extends Thread {
public Handler mHandler;
@Override
public void run() {
Looper.prepare();
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// process incoming messages here
}
};
Looper.loop();
}
}
推荐文章
- Android Studio, logcat在应用程序关闭后清理
- 在android中从上下文获取活动
- 如何从线程捕获异常
- 无法解析主机"<URL here>"没有与主机名关联的地址
- 列表是线程安全的吗?
- getActivity()在Fragment函数中返回null
- 按钮背景是透明的
- 在Mac OS X上哪里安装Android SDK ?
- 我如何获得图像缩放功能?
- 在Android应用程序中显示当前时间和日期
- BottomSheetDialogFragment的圆角
- 在应用程序启动时出现“无法获得BatchedBridge,请确保您的bundle被正确打包”的错误
- 我如何改变默认对话框按钮的文本颜色在安卓5
- 更改单选按钮的圆圈颜色
- 如何在android中复制一个文件?