我有以下函數用于監聽加速度計和磁力計值: // 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。這是為什么?
1 回答

九州編程
TA貢獻1785條經驗 獲得超4個贊
我通過訪問這樣的變量解決了這個問題:LocationUpdater.mGravity
在偵聽器內部,這似乎可以解決它。
public void onSensorChanged(SensorEvent event) { System.arraycopy(event.values, 0, LocationUpdater.mGravity, 0, 3); }
添加回答
舉報
0/150
提交
取消