加速度计传感器导致进入服务时出现损失

12

我正在为 Android Wear 开发一个应用程序。它会监听加速度传感器的坐标并找出一种模式。

为了做到这一点,当用户点击按钮时,服务将启动并开始在列表中存储坐标。通常,加速度传感器每秒记录4到5个坐标。

问题是有时 onSensorChanged()方法在几秒钟内未接收到数据,导致数据丢失和难以找到模式。

这是我的服务要点:https://gist.github.com/cpalosrejano/8f0e59e47124275136fc3d5d941faa07

我尝试过的事情:

  • 我使用android:stopWithTask=false来防止服务在活动死亡时停止。
  • 我还使用了WakeLock来防止设备在服务记录坐标时进入睡眠状态。

我做错了什么?是否有其他方法可以接收来自加速度传感器的回调而不会导致数据丢失?

提前致谢。


你尝试使用不同的 SensorManager.SENSOR_DELAY_* 常量了吗?对问题有影响吗? - Sterling
2个回答

1

有点晚了,但最终我找到了解决方案。

我使用startForeground(int, Notification)方法在前台启动服务,因此服务永远不会停止。通过这个修复,你将永远不会丢失任何SensorEvent。


0
你可以将数据写入设备上的文件,然后从文件中读取。
// When sensor value has changed
@Override
public void onSensorChanged(SensorEvent event){
    if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){

    //Perform a background task to store the data
    new SensorEventLoggerTask().execute(event);

    // And continue to do whatever you want and grab the data
    // The data will be saved in to a file on the device and read it from themre
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接