I'm new to Android and I'm trying to write an app that will send sensor data from a phone over the network to a PC.
I know I shouldn't do 'expensive' operations on the UI thread. Android开发者_运维百科 allows you to register a SensorEventListener with a Handler.
public boolean registerListener (SensorEventListener listener, Sensor sensor, int rate, Handler handler)
Here's the link to the same documentation.
Would anybody be able to explain what the purpose of the Handler is? Should I create a Handler that implements a SensorEventListener? That seems wrong to me, because then I would be passing the same object for two of the arguments (listener & handler).
Thanks!
You can start the Handler from the UI thread (it will be called once the SensorEvent is triggered), and in the handler create a new Thread that sends the sensor data over the network.
精彩评论