Android:哪个线程调用.onSensorChanged? [英] Android: Which thread calls .onSensorChanged?

查看:73
本文介绍了Android:哪个线程调用.onSensorChanged?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了一些讨论,讨论哪些线程调用了各种回调方法,例如与Sensor相关的那些回调方法.大多数人声称UI线程会调用回调-即使涉及一个单独的工作线程也是如此.我们对此有把握吗?

I've read a few discussions about which thread calls various callback methods, for example those associated with Sensors. Most claim that the UI thread calls the callbacks - even when a separate worker thread is involved. Are we CERTAIN about that?

请考虑以下情形:一个单独的类实现Runnable和SensorListener. UI线程(在onCreate期间)启动可运行对象,然后返回其其他业务.现在独立的工作线程在其自己的类中注册了SensorListener.

Consider this scenario: A separate class implements Runnable and SensorListener. The UI thread (during onCreate) starts the runnable and then goes back to its other business. The now-independent worker thread, in its own class, then registers the SensorListener.

请注意,UI线程永远不会与SensorManager或SensorListener进行任何交互.与Sensor无关的唯一线程,以及它唯一了解的类和成员方法,都是工作线程及其类.

Note that the UI thread never has any interaction with SensorManager nor SensorListener. The only thread that ever touches anything to do with Sensor, and the only class and member methods that it ever knows about, is the worker thread and its class.

我很难想象UI线程会在这种情况下调用回调.然而,在线讨论非常有信心".有人知道吗?

It's hard for me to imagine that the UI thread would be calling the callback in this situation. Yet the online discussions are pretty "confident". Anyone know for certain?

谢谢!

推荐答案

我们确定吗?

Are we CERTAIN about that?

是的,尽管这取决于您注册侦听器的方式,并且该行为没有特别有据可查的

Yes, though it depends on how you register the listener, and the behavior is not especially well-documented.

有两种采用SensorEventListenerregisterListener()方法.一个取Handler,另一个不取.后者将使用与主应用程序线程关联的Handler.如果您希望将事件传递到后台线程,请使用HandlerThread(实际上应称为LooperThread,但他们没有问我...),在其中创建Handler并使用HandlerregisterListener().

There are two registerListener() methods that take a SensorEventListener. One takes a Handler, the other does not. The latter one will use a Handler that is associated with the main application thread. If you wish to have the events delivered to a background thread, use a HandlerThread (which really should be called LooperThread, but they didn't ask me...), create a Handler in it, and use that Handler with registerListener().

这篇关于Android:哪个线程调用.onSensorChanged?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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