Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For simpliest applications, it can be solved easily:</p> <pre><code>DatagramSocket socket; public void onSensorChanged() { //calculate and create DatagramPacket here new Thread(new Runnable() { @Override public void run() { socket.send(packet); } }); } </code></pre> <p>It's not pretty, but it works (any calculations in <code>onSensorChanged()</code> aren't pretty to be fair...). Question is, whether the sending thread has to be synchronized with <code>onSensorChanged()</code> method. I figured out, that when using SENSOR_DELAY_GAME, sampling frequency on my SGS3 is around 20 ms, so it is long enough not to be synchronized. Of course it would work correctly only if Your calculations of sensor readings are simple and not time-consuming.</p> <p>But, all in all, calculating any data in <code>onSensorChanged()</code> isn't a good practise. From developer.android.com:</p> <blockquote> <p>Don't block the onSensorChanged() method</p> <p>Sensor data can change at a high rate, which means the system may call the onSensorChanged(SensorEvent) method quite often. As a best practice, you should do as little as possible within the onSensorChanged(SensorEvent) method so you don't block it. If your application requires you to do any data filtering or reduction of sensor data, you should perform that work outside of the onSensorChanged(SensorEvent) method.</p> </blockquote> <p>Therefore what I would suggest is put sensor data in some data container (i.e. <code>List</code> or sth...) and access this data from another thread, calculate it and send.</p> <p>And for the last question: even if there was a way to run <code>onSensorChanged()</code> in separate thread, it would be fatal to most applications, due to possible thousands of threads at one time (as far as I'm concerned, Java is quite restraining environment and there are significant error-preventing mechanisms already on API-level).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload