简体   繁体   English

接近感应器持续时间(Android)

[英]Proximity Sensor Duration (Android)

In the Android API, does anyone know a way to determine how long a proximity sensor is in a given state? 在Android API中,是否有人知道一种确定接近传感器处于给定状态的时间的方法?

For example, I would like for the screen to turn off only if the proximity sensor senses something close to it for more than 2 consecutive seconds. 例如,我希望屏幕仅在接近传感器连续2秒以上感应到接近它的东西时才关闭。

My current code reads the proximity sensor state just fine, but it immediately turns off the screen. 我当前的代码很好地读取了接近传感器的状态,但立即关闭了屏幕。 I would like to wait longer to prevent an accidental finger swipe in front of the sensor from turning off the screen. 我想等待更长的时间,以防止意外的手指在传感器前滑动以关闭屏幕。 Thanks! 谢谢!

You can start a service in background, which collects data from your onSensorChanged() - method (from SensorEventListener ). 您可以在后台启动服务,该服务从onSensorChanged()-方法(从SensorEventListener )收集数据。

Given to that data you can make some if-cases to determine the sensor state. 根据这些数据,您可以进行一些假设判断以确定传感器状态。

尝试使用system.currentTimeMillis()来计数所需的时间,然后在计时器为0时关闭屏幕

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM