Android:onSensorChanged 的​​替代方案?当手机背面放在桌子上时(没有移动时)没有加速度计数据

发布于 2024-11-03 18:28:12 字数 755 浏览 6 评论 0原文

所以我使用这段代码来获取加速度计数据。当我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妖妓 2024-11-10 18:28:12

您可以让一个线程监听传感器变化(即 onSensorChanged() 会将数据写入全局变量),另一个线程运行计时器(查看 postDelayedhttp://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 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.

七度光 2024-11-10 18:28:12

如果仅在“传感器发生变化”时调用此方法,请尝试保存执行时读取的值。如果没有调用该方法并不意味着值是相同的?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文