加速度计传感器导致服务损失 运行

Accelerometer sensor cause losses running into a service

我正在为 Android Wear 开发应用程序。它侦听来自加速度计传感器的坐标并找到模式。

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

问题是有时 onSensorChanged() 方法在几秒钟内没有接收到数据,导致数据丢失和查找模式的麻烦。

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

我尝试过的事情:

我做错了什么?有没有另一种方法可以在不导致数据丢失的情况下接收来自加速度计传感器的回调?

提前致谢。

你可以做的是将数据写入设备上的文件并从文件中读取。

// 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
    }
}

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

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