Android的:替代onSensorChanged?当手机躺在其表背面没有加速度计数据(没有运动) [英] Android: Alternative to onSensorChanged? no ACCELEROMETER data when phone is lying on its back on a table( when no movement)
问题描述
所以我用这个code,以获得加速度的数据。当我在DDMS模式来检查什么我打印语句打印时,我注意到,当手机没有移动分毫打印。 IE浏览器。它在桌上。我想原因是,当手机不动onSensorChanged不叫,然后将其当手机再次开始转动调用。如果我想不管手机是否是移动的还是不捕获数据,我应该使用一些其他的功能?其它方法吗?或任何解决方案?任何有助于AP preciated。
谢谢!
公共无效onSensorChanged(SensorEvent事件){ StringBuffer的BUFF =新的StringBuffer();
buff.append(将String.valueOf(event.timestamp));
buff.append(逗号);
buff.append(将String.valueOf(event.values [0]));
buff.append(逗号);
buff.append(将String.valueOf(event.values [1]));
buff.append(逗号);
buff.append(将String.valueOf(event.values [2])); mCurrentFile.println(buff.toString());
mCurrentFile.write(buff.toString()+'\\ N'); 的System.out.println(**+ buff.toString());}
您可以有一个线程监听传感器变化(即 onSensorChanged()
将写数据转换成全局变量),并运行一个计时器另一个线程(看看 postDelayed
的 http://developer.android.com/reference/android/os/Handler.html ),将在设定的时间阅读。如果没有变化,那么这将再次读取相同的数据。简单,干净,漂亮的电池。
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());
}
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 at postDelayed
, 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.
这篇关于Android的:替代onSensorChanged?当手机躺在其表背面没有加速度计数据(没有运动)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!