简体   繁体   中英

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?

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.

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 ).

Given to that data you can make some if-cases to determine the sensor state.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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