title: Vintage Thermometer component + SensorSimulator on the Android emulator
body: <p>After I found <a href="http://mindtherobot.com/blog/272/android-custom-ui-making-a-vintage-thermometer/" rel="nofollow" title="A Vintage Thermometer">this nice custom component</a> that analogly (is this the right word?) displays the temperature read from the Android device's temperature sensor, I wanted to test it with the emulator to see how it works so I've found <a href="http://code.google.com/p/openintents/wiki/SensorSimulator" rel="nofollow" title="Android Sensor Simulator">this Sensor Simulator</a> and altered the component's source to work with it.</p>
  
  <p>I did manage it to work on en emulated device, however I've done a few lucky guesses and'd like to know what exactly I've done :-)</p>
  
  <p>Here are the changes in thermometer's code I had to make to get it to work in conjunction with the temperature sensor simulator:</p>
  
  <p>After importing the <code>sensorsimulator</code> classes:</p>
  
  <pre><code>import org.openintents.sensorsimulator.hardware.Sensor;
  import org.openintents.sensorsimulator.hardware.SensorEvent;
  import org.openintents.sensorsimulator.hardware.SensorEventListener;
  import org.openintents.sensorsimulator.hardware.SensorManagerSimulator;
  </code></pre>
  
  <p>I had to remove the</p>
  
  <pre><code>import android.hardware.Sensor;
  import android.hardware.SensorEvent;
  import android.hardware.SensorEventListener;
  </code></pre>
  
  <p>Changed</p>
  
  <p><code>private SensorManager getSensorManager()</code></p>
  
  <p>into</p>
  
  <p><code>private SensorManagerSimulator getSensorManager()</code></p>
  
  <p>Then the first problem arised. According to <em>How to use the SensorSimulator in your application</em> I changed the line:</p>
  
  <p><code>return (SensorManager) getContext().getSystemService(Context.SENSOR_SERVICE);</code> </p>
  
  <p>into  </p>
  
  <p><code>return SensorManagerSimulator.getSystemService(this, SENSOR_SERVICE);</code>  </p>
  
  <p>and that was an error. Instad of <code>this</code> I had to use <code>getContext()</code>, I am not sure why was that so - maybe because <code>thermometer</code> class extends <code>View</code> therefore it isn't activity? And instead of <code>SENSOR_SERVICE</code> I had to put <code>Context.SENSOR_SERVICE</code>.</p>
  
  <p>After that, the next change was:</p>
  
  <pre><code>private void attachToSensor() {
      SensorManagerSimulator sensorManager = getSensorManager();
      sensorManager.connectSimulator();
  </code></pre>
  
  <p>Since it seems <code>sensorsimulator</code> doesn't implement <code>getSensorList</code> I had to replace</p>
  
  <pre><code>List&lt;Sensor&gt; sensors = sensorManager.getSensorList(Sensor.TYPE_TEMPERATURE);
  </code></pre>
  
  <p>with</p>
  
  <pre><code>Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_TEMPERATURE);
  </code></pre>
  
  <p>and to remove</p>
  
  <pre><code>if (sensors.size() &gt; 0) {
          Sensor sensor = sensors.get(0);
  </code></pre>
  
  <p>Then, of course I also removed the <code>else</code> statement, so the situation when the temeperature sensor is missing would now probably throw an exception when trying to register listener.</p>
  
  <p>At the end, there was one more change I had to make to get rid of the compile errors, for some reason the implementation of <code>registerListener</code> in <code>Sensor Simulator</code> accepts fewer arguments than android's method so I had to remove the <code>handler</code> parameter from</p>
  
  <pre><code>sensorManager.registerListener(this, sensor, SensorManagerSimulator.SENSOR_DELAY_FASTEST, handler);
  </code></pre>
  
  <p>Here, I'd like to know why this time I didn't have to use <code>getContext()</code> instead of <code>this</code>!?</p>
  
  <p>That's about all I had to change, the last thing was to remove unused vars (handler), unused imports (java.util.List) and remove <code>handler = new Handler();</code>.</p>
  
  <p>I find this example of custom UI component very interesting and hope someone is going to clarify and maybe polish the code.</p>