安卓:getMainLooper()和Looper.myLooper之间的差异() [英] Android: difference between getMainLooper() and Looper.myLooper()

查看:2973
本文介绍了安卓:getMainLooper()和Looper.myLooper之间的差异()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正试图复活一个项目。
有上getMainLooper()...

I'm now trying to resurrect one project. There was an exception on getMainLooper()...

我觉得可能是有一个与MainLooper初始化一个问题,在此之前,增加活套。prepareMainLoop()。

I thought that may be there's a problem with MainLooper initialization and added Looper.prepareMainLoop() before that.

例外告诉我,已经有被抛出该对象的弯针...

Exception telling me that there's already a looper for that object was thrown...

然后我试图取代getMainLooper()与Looper.myLooper()和它的工作...

Then I tried to replace getMainLooper() with Looper.myLooper() and it worked...

但我不明白为什么=)

事实上,我没有得到这两样东西之间的区别。我想,在哪里getMainLooper()在我的项目使用的地方它的应用程序的真正主尺蠖最好的地方,但我得到了我得到了..

In fact I don't get the difference between this two things. I think that on the place where getMainLooper() was used in my project it's the best place for the true main looper of the application but I got what I got..

请解释一下。

感谢您的关注。

推荐答案

不同的是,活套。prepareMainLooper() $ P $尺蠖在主界面ppares线。 Android应用程序通常不会调用这个函数。由于主线程有一个活动,服务提供商或广播接收器不久ppared其尺蠖$ P $开始。

The difference is that Looper.prepareMainLooper() prepares looper in main UI thread. Android applications normally do not call this function. As main thread has its looper prepared long before first activity, service, provider or broadcast receiver is started.

活套。prepare() prepares 尺蠖在当前线程。这个函数被调用后,线程可以调用 Looper.loop()开始以处理程序 S处理消息。

But Looper.prepare() prepares Looper in current thread. After this function is called, thread can call Looper.loop() to start processing messages with Handlers.

所以,你的情况,你有两个线程 - X和Y的X线程是有它的弯针已经美元的Andr​​oid的ppared p $主UI线程。当你在Ÿ线程,你调用活套。prepareMainLooper()你想在x螺纹(主线程)prepare活套。失败的原因是X的弯针已经被prepared。但是,当你调用活套。prepare() Y中的线程,你其实$ Y轴线程p $ pparing尺蠖,因此准备打电话 Looper.loop()

So, in your case you had two threads - X and Y. The X thread is the main UI thread that has its looper already prepared by Android. When you are in Y thread and you're calling Looper.prepareMainLooper() you're trying to prepare looper in X thread (main thread). This fails because X's looper is already prepared. But when you call Looper.prepare() in Y thread, you're actually preparing looper in Y thread and therefore ready to call Looper.loop().

这篇关于安卓:getMainLooper()和Looper.myLooper之间的差异()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆