Collecting, storing, and retrieving sensor data
I'm developing an app which needs access to 'the last 5 seconds worth of data' for a giv开发者_开发问答en sensor. My plan is to store this data somehow, then when I request the data, it'll return any data obtained within the last 5 seconds. I'm not sure how to do this effectively, given that:
Dalvik doesn't like having lots of objects being created, so I can't make an object for each sensor reading which contains the value + timestamp.
I need to be constantly storing new data in some sort of structure, whilst retrieving a specific portion of it.
My only other requirement is that data which is older than 5 seconds should be disposed of. This isn't really critical, but I imagine retaining the data would eat at memory.
I currently have a onSensorChanged
listener set up, so I don't need help with that.
Any ideas?
maybe pre-allocate enough objects to store 5 seconds of data and store them in a pool. get objects from the pool, add time stamp and value and insert into sorted set. remove any old objects after you insert a one and put them back into the pool. or maybe you can get by with just a circular buffer http://en.wikipedia.org/wiki/Circular_buffer
精彩评论