开发者

How does Android accelerometer (Java in general) handles call back listeners?

开发者 https://www.devze.com 2023-01-16 08:44 出处:网络
This question is basic for Java, not android. If the code that is inside listener interface does some complex calculations, what happens for callbacks given by the system. In Android accelerometer rea

This question is basic for Java, not android. If the code that is inside listener interface does some complex calculations, what happens for callbacks given by the system. In Android accelerometer readings are collected in onSensorChanged(SensorEvent event). If I want to process "event" data and that its called around 30-40 times a second. What happens ?

Does this reduces the calls to function? Or 开发者_运维问答Does this lags the output but all call will finally get executed ?

I know this should be handled in separate thread, but if large number of threads keep generating , this may be a problem. Also I cannot rely on Java System.currentTimeMillis(); for pinging every say 500 milliseconds as this is never reliable (in a way it guarantees the function will not called before 500 ms but not maximum time like it may be even after 1000 second, which in my case would be a problem as I need data atleast in 500ms).

Or should I consider TimerTask instead for collecting data every 500 ms?


You cannot do complex calculations anyway in a callback. If it tooks some seconds to complete you should end in an ANR.

Best solution will be to create an other thread and save the values from the event you need into that. In that thread you can process the saved values anytime. Since we are talking about accelerometer, this should be possibly 200-250ms.


I would suggest not to write your time consuming implmentation in any listner.

Even though due to requirement in your app, it is advisable to use any thread independent queue data structure.

put event data in queue and consume it at other end.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号