有没有办法在Android中检索多个传感器数据

发布于 2024-10-05 18:58:47 字数 958 浏览 6 评论 0原文

我正在尝试获取加速度计和方向数据。目前,我创建了 AccelerometerManager 和 OrientationManager。他们都做同样的事情;实现 SensorEventListener 并从 onSensorChanged() 侦听器中的 value[] 检索数据。

有没有更简单的方法来做到这一点?看起来有 2 个带有重复代码的处理程序是没有必要的。有没有办法访问带有组合加速度计和方向数据的values[]数组?

由于有 2 个处理程序,我还必须这样做:

    orientationManager = new OrientationManager(this);
    orientationSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    orientationSensorManager.registerListener(orientationManager, 
            orientationSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), 
            SensorManager.SENSOR_DELAY_GAME);

    accelerometerManager = new AccelerometerManager(this);
    accelerometerSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    accelerometerSensorManager.registerListener(accelerometerManager, 
            orientationSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 
            SensorManager.SENSOR_DELAY_GAME);

I'm trying to get both accelerometer and orientation data. Currently, I created an AccelerometerManager and OrientationManager. They both do the same thing; implement SensorEventListener and retrieve data from values[] in onSensorChanged() listeners.

Is there an easier way to do this? It seems like having 2 handlers with duplicate code is uneccessary. Is there a way to access a values[] array with the combined accelerometer and orientation data together?

Because of the 2 handlers, I'm also having to do this:

    orientationManager = new OrientationManager(this);
    orientationSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    orientationSensorManager.registerListener(orientationManager, 
            orientationSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), 
            SensorManager.SENSOR_DELAY_GAME);

    accelerometerManager = new AccelerometerManager(this);
    accelerometerSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    accelerometerSensorManager.registerListener(accelerometerManager, 
            orientationSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 
            SensorManager.SENSOR_DELAY_GAME);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫腻 2024-10-12 18:58:48

你能尝试这样的事情吗:

private SensorManager mSensorManager;
private SensorEventListener mSensorListener;

////


mSensorManager = (SensorManager) this
                .getSystemService(Context.SENSOR_SERVICE);
mSensorListener = new SensorEventListener() {
    @Override
    public void onAccuracyChanged(Sensor arg0, int arg1) {
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        Sensor sensor = event.sensor;
        if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            //TODO: get values
        }else if (sensor.getType() == Sensor.TYPE_ORIENTATION) {
            //TODO: get values
        }
    }
}

mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_GAME);

Can you try something like this:

private SensorManager mSensorManager;
private SensorEventListener mSensorListener;

////


mSensorManager = (SensorManager) this
                .getSystemService(Context.SENSOR_SERVICE);
mSensorListener = new SensorEventListener() {
    @Override
    public void onAccuracyChanged(Sensor arg0, int arg1) {
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        Sensor sensor = event.sensor;
        if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            //TODO: get values
        }else if (sensor.getType() == Sensor.TYPE_ORIENTATION) {
            //TODO: get values
        }
    }
}

mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_GAME);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文