Android - how to run your sensor ( service, thread, activity )?

后端 未结 1 431
无人共我
无人共我 2021-01-26 16:34

I\'m writing a simple tilting game. Make a ball roll over the screen in the direction you tilt your phone.

So I downloaded an example on how to get the sensor data and t

相关标签:
1条回答
  • 2021-01-26 16:43

    In my humble experience, you should:

    A) Register your SensorListener on your main thread normally, but make it spawn two (or as needed) WorkerThreads on its constructor.

    B) Get the looper reference of both threads, to keep them alive waiting for future requests.

    B) Listen to your onSensorChanged on your main thread normally, but do NOTHING OTHER (this is important) then delegate a copy of the values to a new runnable object that will process your game logic.

    C) Enqueue your new runnable object on your WorkerThread looper reference accordingly.

    D) Make the runnables (that are already off your main thread) do what they have to do and synchronize changes to a singleton that may be consulted by who is interested in updating your views.

    0 讨论(0)
提交回复
热议问题