Android:onSensorChanged 的替代方案?当手机背面放在桌子上时(没有移动时)没有加速度计数据
所以我使用这段代码来获取加速度计数据。当我在 DDMS 模式下检查打印语句正在打印的内容时,我注意到当手机不动时没有打印任何内容。 IE。它在桌子上。我认为原因是当手机不移动时不会调用 onSensorChanged ,然后当手机再次开始移动时调用 onSensorChanged 。如果我想无论手机是否移动都捕获数据,我应该使用其他功能吗?有其他选择吗?或者有什么解决办法吗?任何帮助表示赞赏。
谢谢!
public void onSensorChanged(SensorEvent event) {
StringBuffer buff = new StringBuffer();
buff.append(String.valueOf(event.timestamp));
buff.append(comma);
buff.append(String.valueOf(event.values[0]));
buff.append(comma);
buff.append(String.valueOf(event.values[1]));
buff.append(comma);
buff.append(String.valueOf(event.values[2]));
mCurrentFile.println(buff.toString());
mCurrentFile.write(buff.toString()+ '\n');
System.out.println( "**" + buff.toString());
}
So I'm using this code to get the Accelerometer data. When I am in the DDMS mode to check what my print statement is printing, I noticed that nothing is printed when the phone does not move. ie. its on the table. I think the reason is that onSensorChanged is not called when the phone does not move, and then its called when the phone starts moving again. If i want to capture data regardless of whether the phone is moving or not, should I use some other function? Any Alternatives? or Any solution? Any helps appreciated.
Thanks!
public void onSensorChanged(SensorEvent event) {
StringBuffer buff = new StringBuffer();
buff.append(String.valueOf(event.timestamp));
buff.append(comma);
buff.append(String.valueOf(event.values[0]));
buff.append(comma);
buff.append(String.valueOf(event.values[1]));
buff.append(comma);
buff.append(String.valueOf(event.values[2]));
mCurrentFile.println(buff.toString());
mCurrentFile.write(buff.toString()+ '\n');
System.out.println( "**" + buff.toString());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以让一个线程监听传感器变化(即
onSensorChanged()
会将数据写入全局变量),另一个线程运行计时器(查看postDelayed
, http://developer.android.com/reference/android/os/Handler.html ),它将在设定的时间读取它。如果没有变化,那么它将再次读取相同的数据。简单、干净且对电池有益。You can have one thread listening to the sensor changes (i.e.,
onSensorChanged()
that will write the data into global variables) and another thread running a timer (look atpostDelayed
, http://developer.android.com/reference/android/os/Handler.html) that will read it at set times. If there are no changes, then it will read the same data again. Easy, clean, and nice to the battery.如果仅在“传感器发生变化”时调用此方法,请尝试保存执行时读取的值。如果没有调用该方法并不意味着值是相同的?
If this method is only called when "the sensor changes", try saving the values you read when it's executed. If the method is not called that doesn't mean that the values are the same?