Android 加速度计数据丢失

Android 加速度计数据丢失,android,service,accelerometer,sensors,sensormanager,Android,Service,Accelerometer,Sensors,Sensormanager,我的问题如下: 我正在开发一个应用程序,它只需将加速度计样本存储在一个阵列中,然后分析这些数据以找到行走模式。我编写了以下服务: public class gService extends Service implements SensorEventListener { private SensorManager senSensorManager; private Sensor senAccelerometer; private static final String

我的问题如下: 我正在开发一个应用程序,它只需将加速度计样本存储在一个阵列中,然后分析这些数据以找到行走模式。我编写了以下服务:

public class gService extends Service implements SensorEventListener {

    private SensorManager senSensorManager;
    private Sensor senAccelerometer;
    private static final String TAG = "MEDIA";
    private StepAnalyzer analyzer;
    private int numPass;

    public BroadcastReceiver mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // Check action just to be on the safe side.
            if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                //Log.v("shake mediator screen off","trying re-registration");
                // Unregisters the listener and registers it again.
                senSensorManager.unregisterListener(gService.this);
                senSensorManager.registerListener(gService.this, senAccelerometer,SensorManager.SENSOR_DELAY_GAME);
            }
        }
    };

    @Override
    public void onCreate() {
        Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
        senSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        senAccelerometer = senSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        senSensorManager.registerListener(this, senAccelerometer , SensorManager.SENSOR_DELAY_GAME);
        analyzer = new StepAnalyzer(getApplicationContext());
        numPass = 1;
        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
        registerReceiver(mReceiver, filter);
    }

    @Override
    public void onDestroy(){
        Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
        senSensorManager.unregisterListener(this);
        unregisterReceiver(mReceiver);
    }

    @Override
    public void onStart(Intent intent, int startid){
        Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    @Override
    public void onSensorChanged(SensorEvent sensorEvent){
        Sensor mySensor = sensorEvent.sensor;
        if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            float x = sensorEvent.values[0];
            float y = sensorEvent.values[1];
            float z = sensorEvent.values[2];
            float gNorm = ((float)Math.sqrt(x * x + y * y + z * z))/ SensorManager.STANDARD_GRAVITY ;
            long time = sensorEvent.timestamp / 1000000;
            ......
            // other operations

        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy){

    }

    @Override
    public IBinder onBind(Intent intent) {
        // We don't provide binding, so return null
        return null;
    }


}
问题是,当屏幕打开时,加速计以正确的频率提供样本(20ms->SENSOR_DELAY_GAME),而当屏幕关闭时,加速计样本以“随机”方式提供,间隔为20ms,间隔最长为20s。 正如您在代码中看到的,我已经实现了在相关问题中看到的解决方法:捕获屏幕关闭事件,取消注册并重新注册侦听器,但没有任何运气

我还发现这种行为可能与我的手机有关,我有一款摩托罗拉Moto X 2014版

为了解释这一点,我附上了两个连续传感器事件之间的时间戳差异图。

在X轴->样品的#上 在Y轴上->两个连续采样之间的时间(毫秒)


非常感谢。

显然这是很常见的行为,我自己也看到过。还有。取决于你的应用程序。使用a保持显示器打开可能是一个选项。是的,这可能是一个选项,但如果可能的话,我正在寻找更好的解决方案。这是我最后的选择。谢谢你的回答