如何在另一项活动中使用传感器数据(现在,由于您的帮助,它运行良好!)

时间:2016-03-11 17:17:20

标签: android sensor android-sensors

(现在它完美无缺)我是Android开发的新手,我编写了一个showActivity来显示来自另一个类的结果数据。在该类中,我实现了一个sensorEventListener并获取传感器数据。但是当我想在showActivity中显示数据时,我什么都没有显示出来。所有数据都是0(或错误),我不知道我的代码有什么问题。如果您能就如何解决问题给我一些建议,我们将不胜感激。非常感谢。 这是我的showActivity:

public class MapShowActivity extends AppCompatActivity {


public SensorUse mySensorUse ;
private TextView myText;
private Button myButton;
private Timer timer = new Timer();
private int count = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mySensorUse = new SensorUse(MapShowActivity.this);
    setContentView(R.layout.activity_map_show);
    myText = (TextView)findViewById(R.id.orientation);
}

public void startTimer(){
    timer.scheduleAtFixedRate(detectNewOrientation, 100, 100);
}

TimerTask detectNewOrientation = new TimerTask(){

    @Override
    public void run() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                myText.setText(String.valueOf(mySensorUse.getOx()));
            }
        });
    }
};

@Override
protected void onResume() {
    super.onResume();
    mySensorUse.sensorRegister(MapShowActivity.this);
    startTimer();

}

@Override
protected void onPause() {
    super.onPause();
    mySensorUse.sensorUnregister();
}

}

这是我的传感器课程:

public class SensorUse implements SensorEventListener{
private float[] accValues = new float[3];
private float[] magValues = new float[3];
private float[] orientationValues = new float[4];
private float[] resultRotationMatrix = new float[16];
public float ax;
public float ay;
public float az;
public float sumAcc;
public float ox;
public static boolean flag = false;
public boolean rotationMatrixGenerated;
private SensorManager mySensorManager;
private Sensor accSencor, magSensor;

public SensorUse(Context context){



}

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_LINEAR_ACCELERATION){
        accValues = event.values.clone();
        ax = accValues[0];
        ay = accValues[1];
        az = accValues[2];
        sumAcc = (float) Math.sqrt(ax*ax + ay*ay + az*az);
        flag = true;
    }
    else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD){
        magValues = event.values.clone();
    }
    if (accValues != null && magValues != null){
        rotationMatrixGenerated = SensorManager.getRotationMatrix(resultRotationMatrix,
                null, accValues, magValues);
        if(rotationMatrixGenerated) {
            SensorManager.getOrientation(resultRotationMatrix, orientationValues);
            ox = (float) Math.toDegrees(orientationValues[0]);
        }
    }

}

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

}

public void sensorRegister(Context context){
    mySensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
    accSencor = mySensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
    magSensor = mySensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    mySensorManager.registerListener(this, accSencor, SensorManager.SENSOR_DELAY_GAME);
    mySensorManager.registerListener(this, magSensor, SensorManager.SENSOR_DELAY_GAME);
}

public void sensorUnregister(){
    mySensorManager.unregisterListener(this, accSencor);
    mySensorManager.unregisterListener(this, magSensor);
}
public float getAx(){
    return ax;
}
public float getOx(){
    return ox;
}

}

1 个答案:

答案 0 :(得分:0)

App不知道哪个类将处理传感器数据。
您必须注册传感器类(SensorUse)对象 得到加速度计数据。

查找以下示例代码:

sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
senAccelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorManager.registerListener(mySensorUse, senAccelerometer , SensorManager.SENSOR_DELAY_NORMAL);
  1. sensorManger是SensorManager的对象
  2. mySensorUse是已实现SensorEventListener
  3. 的类