我正在为Android磨损开发一个应用程序。它监听加速度计传感器的坐标,并找到一个图案。
为此,当用户单击按钮时,服务将启动并开始在列表中存储坐标。通常,加速度计传感器每秒记录4到5个坐标。
问题是,有时onSensorChanged()方法在几秒钟内没有接收数据,导致数据丢失,难以找到模式。
以下是我服务的要点:https://gist.github.com/cpalosrejano/8f0e59e47124275136fc3d5d941faa07
我尝试过的事情:
我做错了什么?是否有另一种方法可以在不造成数据丢失的情况下接收加速度传感器的回调?
提前谢谢。
发布于 2016-12-27 11:44:40
有点晚了,但最终我找到了解决办法。
我在前台启动服务,使用方法startForeground(int, Notification),这样服务就不会停止。使用此修补程序,您将永远不会丢失任何SensorEvent。
发布于 2016-04-28 12:28:49
您可以做的是将数据写入设备上的文件并从文件中读取数据。
// 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
}
}https://stackoverflow.com/questions/36384252
复制相似问题