使用event.values作为应用程序的多个传感器无法识别差异

时间:2019-04-17 12:26:35

标签: android android-sensors

我正在尝试显示来自两个不同传感器的数据,如果我注释掉了加速度计数据,则该应用将运行并显示光传感器数据,但是尝试显示所显示的加速度计数据会导致其在启动时崩溃。如果我注释掉与光传感器有关的所有内容,则确实会显示加速度计数据,但无法同时显示它们。

问题是两个传感器都使用event.values,但至少从我自己的研究来看,这应该可以工作,但是如果在sensorChanged上输入错误, Get multiple sensor data at the same time in Android建议如何解决此问题 任何帮助都很好

     public class MainActivity extends AppCompatActivity implements SensorEventListener {

//System sensor manager
SensorManager sensorManager;
//active sensors
Sensor lightSensor;
Sensor acelSensor;

TextView xText, yText, zText, lightS;


double ax,ay,az;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //initulise veriables
    lightS = (TextView) findViewById(R.id.lightS);
    xText = (TextView) findViewById(R.id.xText);
    yText = (TextView) findViewById(R.id.yText);
    zText = (TextView) findViewById(R.id.zText);
    //instance of sensor manager

    sensorManager = (SensorManager) getSystemService(Service.SENSOR_SERVICE);

    //get sensors from sensor manager
    lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
    acelSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

    //if error
    if (lightSensor == null) { lightS.setText("sensor_error"); }
    if (acelSensor == null) {
        xText.setText("sensor_error");
}}

@Override
public void onStart() {
    super.onStart();

    if (lightSensor !=null)
    {
        sensorManager.registerListener(this, lightSensor, SensorManager.SENSOR_DELAY_FASTEST);
    }
    if (acelSensor !=null)
    {
        sensorManager.registerListener(this, acelSensor, SensorManager.SENSOR_DELAY_FASTEST);
    }
}
@Override
public void onPause() {
    super.onPause();
    sensorManager.unregisterListener(this);
}

@Override
public void onResume() {
    super.onResume();
    sensorManager.registerListener(this, lightSensor, SensorManager.SENSOR_DELAY_FASTEST);
    sensorManager.registerListener(this, acelSensor, SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
public void onSensorChanged(SensorEvent event) {


        if (event.sensor.getType() == Sensor.TYPE_LIGHT) ;
        {
            float lux = event.values[0];
            lightS.setText(String.valueOf(lux));
        }
          if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) ;{
            /*ax=event.values[0];
            ay=event.values[1];
            az=event.values[2];*/
            xText.setText("X:   " +  event.values[0]);
            yText.setText("Y:   " + event.values[1]);
            zText.setText("Z:   " + event.values[2]);
        }
    }

1 个答案:

答案 0 :(得分:0)

您要两次注册传感器,一次是在onstart(),一次是在onresume()。

我删除了多余的onregistersensor()。

     public class MainActivity extends AppCompatActivity implements SensorEventListener {

//System sensor manager
SensorManager sensorManager;
//active sensors
Sensor lightSensor;
Sensor acelSensor;

TextView xText, yText, zText, lightS;

double x;
double y;
double z;
double ax, ay, az;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //initulise veriables
    //       lightS = (TextView) findViewById(R.id.lightS);
    //       xText = (TextView) findViewById(R.id.xText);
    //      yText = (TextView) findViewById(R.id.yText);
    //       zText = (TextView) findViewById(R.id.zText);
    //instance of sensor manager

    sensorManager = (SensorManager) getSystemService(Service.SENSOR_SERVICE);



}

@Override
public void onStart() {
    super.onStart();


}

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

@Override
public void onResume() {
    super.onResume();

    sensorManager.registerListener(this,
            sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
            SensorManager.SENSOR_DELAY_UI);

    sensorManager.registerListener(this,
            sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT),
            SensorManager.SENSOR_DELAY_UI);
}

@Override
public void onSensorChanged(SensorEvent event) {


    if (event.sensor.getType() == Sensor.TYPE_LIGHT) ;
    {
        float lux = event.values[0];

        System.out.println("lux " + lux);
    }
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        x = event.values[0] /9.8;
        y = event.values[1] /9.8;
        z = event.values[2] /9.8;

        System.out.println("x: " + x);
        System.out.println("y: " + y);
        System.out.println("z: " + z);


    }
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}

}