tags:

views:

1642

answers:

2

Hi,

I am trying to create an application which reads data from digital compass. I tried to reuse the code from the book Professional Android Application Development but the IDE displayed a note

The type SensorListener is deprecated

I guess it is because the code from the book is written for the earlier SDK version so I tried to use SensorEventListener instead.

Then when I tried to register the listener

sensorManager.registerListener(sensorListener, SensorManager.SENSOR_ORIENTATION, SensorManager.SENSOR_DELAY_FASTEST);

an error appeared:

The method registerListener(SensorListener, int, int) in the type SensorManager is not applicable for the arguments (SensorEventListener, int, int)

so I tried to cast SensorEventListener to SensorListener, but the application doesn't work.

Does anyone know how to use sensor in newer SDK versions?

Thank you.

+3  A: 

There is a separate SensorEventListener class you need to use. See here and here and here for samples.

CommonsWare
+3  A: 

You actually need to pass in a Senor object, not just the ID of it.

Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); sensorManager.registerListener(sensorListener, sensor, SensorManager.SENSOR_DELAY_FASTEST)

CaseyB