我有以下函数用于监听加速度计和磁力计值:
// Storage for Sensor readings
public float[] mGravity = new float[3];
public float[] mGeomagnetic = new float[3];
public void registerSensors(Context context) {
// First, get an instance of the SensorManager
SensorManager sMan = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
// Second, get the sensor you're interested in
Sensor magnetField = sMan.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
// Get a reference to the accelerometer
Sensor accelerometer = sMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
// Third, implement a SensorEventListener class
SensorEventListener magnetListener = new SensorEventListener() {
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// do things if you're interested in accuracy changes
}
public void onSensorChanged(SensorEvent event) {
mGravity = new float[3];
//Log.i("LocationUpdater", "magnetometer updated");
System.arraycopy(event.values, 0, mGravity, 0, 3);
//Log.i("LocationUpdater", Float.toString(mGravity[0]));
}
};
SensorEventListener accelListener = new SensorEventListener() {
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// do things if you're interested in accuracy changes
}
public void onSensorChanged(SensorEvent event) {
mGeomagnetic = new float[3];
//Log.i("LocationUpdater", "accelerometer updated");
System.arraycopy(event.values, 0, mGeomagnetic, 0, 3);
}
};
但是,从传感器读取的值不会写入数组,所有值都会保留0。这是为什么?
九州编程
相关分类