亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

onSensorChanged() 中的值不會存儲在變量中

onSensorChanged() 中的值不會存儲在變量中

斯蒂芬大帝 2024-01-25 10:34:15
我有以下函數用于監聽加速度計和磁力計值:  // 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);
 }


查看完整回答
反對 回復 2024-01-25
  • 1 回答
  • 0 關注
  • 152 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號