android SensorEventListener problem

后端 未结 4 480
长情又很酷
长情又很酷 2021-01-07 10:50

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

相关标签:
4条回答
  • 2021-01-07 11:08

    The method is just deprecated, you have to use

    registerListener(SensorEventListener, Sensor, int) 
    

    instead.

    0 讨论(0)
  • 2021-01-07 11:10

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

    0 讨论(0)
  • 2021-01-07 11:19

    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)

    0 讨论(0)
  • I had the same problem but when I casted the first 2 parameters as (SensorEventListener) and (Sensor) it worked. I then realised the problem was that for some reason I had declared the Sensor as type "Object" and not "Sensor", so Eclipse failed to identify the types of parameters.

    This worked for me:

    mSensorManager.registerListener((SensorManager)this, (Sensor)mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
    

    But now I have correctly declared mAccelerometer as type Sensor I no longer need the casts.

    0 讨论(0)
提交回复
热议问题