Android: How to collect sensor values for a fixed period of time?

拜拜、爱过 提交于 2019-12-25 02:17:13

问题


I want to collect for ca. 5 seconds the values of the Accelerometer. What is a proper way to do this?

I tried it by implementing an AsyncTask which registers and unregisters itself at the sensor manager for the accelerometer and then sleeps for 5 seconds. But during those 5s of sleeping the onSensorChanged method isn't invoked :D...

The motivation behind my sought is the following: In order to calibrate my accelerometer I want to collect for some seconds sensor values (while the phone is lying on the floor) and then get the bias by computing the mean values...

I appreciate any hint!


回答1:


Assuming you are starting your sensors in onResume:

/**
 * start sensors
 */
public void onResume() {
    mSensorManager.registerListener(mListener, mSensorAcceleration, SensorManager.SENSOR_DELAY_GAME);
    mSensorManager.registerListener(mListener, mSensorMagnetic, SensorManager.SENSOR_DELAY_GAME);
    Handler h = new Handler();
    h.postDelayed(new Runnable() {

        @Override
        public void run() {
        //    do stuff with sensor values
            mSensorManager.unregisterListener(mListener);               
        }
    }, 5000);
...


来源:https://stackoverflow.com/questions/10119297/android-how-to-collect-sensor-values-for-a-fixed-period-of-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!