2016-04-03 94 views
12

我正在开发一个应用程序Android Wear。它从加速度传感器侦听坐标并找到一个模式。加速度计传感器导致服务损失

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

问题是,有时onSensorChanged()方法在数秒内不会接收数据,导致数据丢失并且无法找到模式。

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

事情我已经尝试:

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

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

在此先感谢。

+0

您是否尝试过使用不同的'SensorManager.SENSOR_DELAY_ *'常量?对这个问题有什么影响? – String

回答

0

它有点晚,但finnaly我找到了解决方案。

我在前台启动服务,使用方法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 
    } 
}