How to convert a program that uses SensorListener to SensorEventListener

吃可爱长大的小学妹 提交于 2020-01-13 06:08:09

问题


I am creating an application named CompassApp. I have created two classes named Compass.java and Rose.java. This application has been created from a tutorial described in a website and this uses the deprecated version of SensorListener. The class Compass.java is using this deprecated version. The app worked fine when I used the deprecated version. Then I thought to convert my app into the current version that is SensorEventListener. But the rotation of the compass stopped! I'm stuck and don't know what to do. Please help me.

Compass.java with deprecated SensorListener interface

  public class Compass extends Activity implements SensorListener {
  SensorManager sensorManager;
  static final int sensor = SensorManager.SENSOR_ORIENTATION;
  MyRose rose;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Set full screen view
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    rose = new MyRose(this);

    setContentView(rose);

    // get sensor manager
    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
  }

  // register to listen to sensors
  @Override
  public void onResume() {
    super.onResume();
    sensorManager.registerListener(this, sensor);
  }

  // unregister
  @Override
  public void onPause() {
    super.onPause();
    sensorManager.unregisterListener(this);
  }

  // Ignore for now
  public void onAccuracyChanged(int sensor, int accuracy) {
  }

  // Listen to sensor and provide output
  public void onSensorChanged(int sensor, float[] values) {
    if (sensor != Compass.sensor)
      return;
    int orientation = (int) values[0];
    rose.setDirection(orientation);
  }
 }

Compass.java with new SensorEventListener

     public class Compass extends Activity{
          SensorManager sensorManager;
          SensorEventListener    listener ;

          static final int sensor = SensorManager.SENSOR_ORIENTATION;
          Rose rose;

          /** Called when the activity is first created. */
          @Override
          public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);

           // Set full screen view
           getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
           WindowManager.LayoutParams.FLAG_FULLSCREEN);
           requestWindowFeature(Window.FEATURE_NO_TITLE);

           listener = new SensorEventListener()
           {
              @Override
          public void onSensorChanged(SensorEvent e)
          {   
              int orientation = (int) e.values[0];
              rose.setDirection(orientation);
          } 

          @Override
          public void onAccuracyChanged(Sensor arg0, int arg1) {
      // TODO Auto-generated method stub

       }
        };


        setContentView(rose);

       // get sensor manager
       sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
       sensorManager.registerListener(listener,       sensorManager.getDefaultSensor(sensor),SensorManager.SENSOR_MAGNETIC_FIELD);

      }

       // register to listen to sensors
       @Override
      public void onResume() {
      super.onResume();
      sensorManager.registerListener(listener, sensorManager.getDefaultSensor(sensor), SensorManager.SENSOR_MAGNETIC_FIELD);
      }

       // unregister
       @Override
       public void onPause() {
       super.onPause();
       sensorManager.unregisterListener(listener);
       }
     }

回答1:


I think this is what you need.

public class Compass extends Activity implements SensorEventListener {
  SensorManager sensorManager;
  static final int sensor = SensorManager.SENSOR_ORIENTATION;
  MyRose rose;
  private Sensor myCompassSensor;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Set full screen view
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    rose = new MyRose(this);

    setContentView(rose);

    // get sensor manager
    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    // get compass sensor (ie magnetic field)
    myCompassSensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
  }

  // register to listen to sensors
  @Override
  public void onResume() {
    super.onResume();
    sensorManager.registerListener(this, myCompassSensor, SensorManager.SENSOR_DELAY_NORMAL);
  }

  // unregister
  @Override
  public void onPause() {
    super.onPause();
    sensorManager.unregisterListener(this);
  }

  // Ignore for now
  public void onAccuracyChanged(Sensor sensor, int accuracy) {
      // TODO Auto-generated method stub
  }

  public void onSensorChanged(SensorEvent event) {
      // this check is unnecessary with only one registered sensor
      // but it's useful to know in case you need to add more sensors
      if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
          int orientation = (int) event.values[0];
          rose.setDirection(orientation);
      }
  }
}

Edit: added check to determine sensor type



来源:https://stackoverflow.com/questions/6394192/how-to-convert-a-program-that-uses-sensorlistener-to-sensoreventlistener

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!