本文介绍了如何转换使用SensorListener到SensorEventListener程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个名为CompassApp的应用程序。我已经创建了一个名为Compass.java和Rose.java两班。此应用程序已创建从一个网站上描述的教程,这使用SensorListener的德precated版本。该类Compass.java使用本德precated版本。该应用程序工作得很好,当我用德precated版本。后来我想转换我的应用程序到当前版本是SensorEventListener。但是,罗盘的旋转停止!我坚持,不知道该怎么办。请帮我。

Compass.java与去precated SensorListener接口

 公共类指南针延伸活动实现SensorListener {
  的SensorManager的SensorManager;
  静态最终诠释传感器= SensorManager.SENSOR_ORIENTATION;
  MyRose上涨;

  / **第一次创建活动时调用。 * /
  @覆盖
  公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    //设置全屏视图
    getWindow()。setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    玫瑰=新MyRose(本);

    的setContentView(玫瑰);

    //获取传感器管理
    的SensorManager =(的SensorManager)getSystemService(SENSOR_SERVICE);
  }

  //注册听传感器
  @覆盖
  公共无效onResume(){
    super.onResume();
    sensorManager.registerListener(这一点,传感器);
  }

  //注销
  @覆盖
  公共无效的onPause(){
    super.onPause();
    sensorManager.unregisterListener(本);
  }

  //忽略现在
  公共无效onAccuracyChanged(INT传感器,诠释精度){
  }

  //听传感器,并提供输出
  公共无效onSensorChanged(INT传感器,浮动[]值){
    如果(传感器!= Compass.sensor)
      返回;
    INT取向=(int)的值[0];
    rose.setDirection(方向);
  }
 }
 

Compass.java新SensorEventListener

 公共类指南针延伸活动{
          的SensorManager的SensorManager;
          SensorEventListener监听;

          静态最终诠释传感器= SensorManager.SENSOR_ORIENTATION;
          玫瑰玫瑰;

          / **第一次创建活动时调用。 * /
          @覆盖
          公共无效的onCreate(包savedInstanceState){
           super.onCreate(savedInstanceState);

           //设置全屏视图
           getWindow()。setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
           WindowManager.LayoutParams.FLAG_FULLSCREEN);
           requestWindowFeature(Window.FEATURE_NO_TITLE);

           监听=新SensorEventListener()
           {
              @覆盖
          公共无效onSensorChanged(SensorEvent E)
          {
              INT取向=(int)的e.values​​ [0];
              rose.setDirection(方向);
          }

          @覆盖
          公共无效onAccuracyChanged(传感器为arg0,INT ARG1){
      // TODO自动生成方法存根

       }
        };


        的setContentView(玫瑰);

       //获取传感器管理
       的SensorManager =(的SensorManager)getSystemService(SENSOR_SERVICE);
       sensorManager.registerListener(监听器,sensorManager.getDefaultSensor(传感器),SensorManager.SENSOR_MAGNETIC_FIELD);

      }

       //注册听传感器
       @覆盖
      公共无效onResume(){
      super.onResume();
      sensorManager.registerListener(监听器,sensorManager.getDefaultSensor(传感器),SensorManager.SENSOR_MAGNETIC_FIELD);
      }

       //注销
       @覆盖
       公共无效的onPause(){
       super.onPause();
       sensorManager.unregisterListener(听众);
       }
     }
 

解决方案

我想这是你所需要的。

 公共类指南针延伸活动实现SensorEventListener {
  的SensorManager的SensorManager;
  静态最终诠释传感器= SensorManager.SENSOR_ORIENTATION;
  MyRose上涨;
  私人传感器myCompassSensor;

  / **第一次创建活动时调用。 * /
  @覆盖
  公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    //设置全屏视图
    getWindow()。setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    玫瑰=新MyRose(本);

    的setContentView(玫瑰);

    //获取传感器管理
    的SensorManager =(的SensorManager)getSystemService(SENSOR_SERVICE);
    //获取罗盘传感器(如磁场)
    myCompassSensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
  }

  //注册听传感器
  @覆盖
  公共无效onResume(){
    super.onResume();
    sensorManager.registerListener(这一点,myCompassSensor,SensorManager.SENSOR_DELAY_NORMAL);
  }

  //注销
  @覆盖
  公共无效的onPause(){
    super.onPause();
    sensorManager.unregisterListener(本);
  }

  //忽略现在
  公共无效onAccuracyChanged(传感器传感器,诠释精度){
      // TODO自动生成方法存根
  }

  公共无效onSensorChanged(SensorEvent事件){
      //这个检查是多余的,只有一个注册的传感器
      //但它需要了解的情况下,你需要添加更多的传感器
      如果(event.sensor.getType()== Sensor.TYPE_MAGNETIC_FIELD){
          INT取向=(int)的event.values​​ [0];
          rose.setDirection(方向);
      }
  }
}
 

编辑:增加了检查,以确定传感器类型

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);
       }
     }
解决方案

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

这篇关于如何转换使用SensorListener到SensorEventListener程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 16:14