Question

I am working on a android application where shake detection is required, which is already implemented and working perfect. I am using Accelerometer to detect shake.

Here is my question:

Current implementation is using/listening sensor on main UI thread, which is working perfect as of now. But still I am confused about the best practice to use the sensor, what it is? to run on Main Thread or on Separate Thread?

if Main Thread, its pretty clear me.

if your suggestion is Separate Thread, then why so?

One more thing, on the name of processing after shake detection, I am doing network request which is on Separate Thread, so no load of after detecting shake, only thing I care about is how costly it is to Start Listening to a sensor on main thread.

Était-ce utile?

La solution

With simple sensor detection it is more than adequate to use the main UI thread. Starting up a separate thread with sensors would require a handler and thread cleanup. You are adding unnecessary steps.

If you are already pushing your ui you are likely better off moving some of your other bulk off of the main thread. Sensors are just handled really efficiently with Android already, moving it off the thread won't help as much as it could hurt if you aren't careful.

Now performing operations on the data you get from sensors (like location from GPS) is a whole different ballgame, and something that will very possibly need it's own thread.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top