Issue
I'm reading Sensor Data from Device Sensors(Multiple Sensors) and call a Method to show the data I received from sensors to the designed TextView.
The method I'm calling to update TextView:
private void UpdateDataStream(String _Entry,Boolean isAppend)
{
if(isAppend)
ViewHolder.append("\n("+sensorName+")"+_Entry);
else
ViewHolder.setText("("+sensorName+")"+_Entry);
}
Method called from SensorEventListener's , onSensorChanged Event.
What I couldn't figure out is how to prevent freeze while updating TextView; since sensor data update is intensive(considering it is from multiple sensors simultaneously) updating TextView cause Application to freeze or crash. I don't need to "print" every data I received, like It is enough for me to print for every 1-2 second, lost data can be ignored.
Is there any approach/patern or built-in structure to achieve this ? (I'm new to programming in Java&Android, yet I'm familiar with multiprogramming, parallel programming concepts, yet I couldn't figure out how to apply them in this environment)
Solution
This is fastest solution: https://github.com/ReactiveX/RxJava http://reactivex.io/documentation/operators/debounce.html
Or you can:
- create stack (for example)
- write sensors data stream to stack from
thread#1
- in another infinite loop
thread#2
read last data from stack every N time (don't forget to synchronize them) - clean the stack
- write last data to TextView (don't forget to set text in ui thread:
textView.post( () -> textView.setText("safe call") );
Answered By - edwardstock
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.