Vintage Thermometer component + SensorSimulator on

2019-01-26 01:35发布

After I found this nice custom component 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 this Sensor Simulator and altered the component's source to work with it.

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 :-)

Here are the changes in thermometer's code I had to make to get it to work in conjunction with the temperature sensor simulator:

After importing the sensorsimulator classes:

import org.openintents.sensorsimulator.hardware.Sensor;
import org.openintents.sensorsimulator.hardware.SensorEvent;
import org.openintents.sensorsimulator.hardware.SensorEventListener;
import org.openintents.sensorsimulator.hardware.SensorManagerSimulator;

I had to remove the

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;

Changed

private SensorManager getSensorManager()

into

private SensorManagerSimulator getSensorManager()

Then the first problem arised. According to How to use the SensorSimulator in your application I changed the line:

return (SensorManager) getContext().getSystemService(Context.SENSOR_SERVICE);

into

return SensorManagerSimulator.getSystemService(this, SENSOR_SERVICE);

and that was an error. Instad of this I had to use getContext(), I am not sure why was that so - maybe because thermometer class extends View therefore it isn't activity? And instead of SENSOR_SERVICE I had to put Context.SENSOR_SERVICE.

After that, the next change was:

private void attachToSensor() {
    SensorManagerSimulator sensorManager = getSensorManager();
    sensorManager.connectSimulator();

Since it seems sensorsimulator doesn't implement getSensorList I had to replace

List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_TEMPERATURE);

with

Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_TEMPERATURE);

and to remove

if (sensors.size() > 0) {
        Sensor sensor = sensors.get(0);

Then, of course I also removed the else statement, so the situation when the temeperature sensor is missing would now probably throw an exception when trying to register listener.

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 registerListener in Sensor Simulator accepts fewer arguments than android's method so I had to remove the handler parameter from

sensorManager.registerListener(this, sensor, SensorManagerSimulator.SENSOR_DELAY_FASTEST, handler);

Here, I'd like to know why this time I didn't have to use getContext() instead of this!?

That's about all I had to change, the last thing was to remove unused vars (handler), unused imports (java.util.List) and remove handler = new Handler();.

I find this example of custom UI component very interesting and hope someone is going to clarify and maybe polish the code.

0条回答
登录 后发表回答