Detect user proximity from the screen

邮差的信 提交于 2019-12-10 13:52:38

问题


I'm working on a BB10 app that needs to be able to disable the screen when the user holds the phone up to his/her face during a call.

How can I tell when the user is holding the phone up to his/her face?


回答1:


To detect the user's proximity from the phone, you can use a QProximitySensor.

In order to use this, you need to add these lines to your project's .pro file:

CONFIG += mobility
MOBILITY += sensors

Add the necessary includes to the .cpp and .h files:

#include <QtSensors/QProximitySensor>
using QtMobility::QProximitySensor;

#include <QtSensors/QProximityReading>
using QtMobility::QProximityReading;

Define the proximity sensor in the .h file. Create and destroy the sensor in your constructor and destructor functions.

When the call starts, connect your sensor's readingChanged function to the one that you intend to handle the reading, and activate it:

connect(proximitySensor, SIGNAL(readingChanged()), this, SLOT(checkReading()));
proximitySensor->setActive(true);

When the call ends, deactivate the sensor:

proximitySensor->setActive(false);

Finally, use the reading's close function to tell when the device is close to the user's face. Note that the distance defined as "close" may be different for different devices.

bool isClose = proximitySensor->reading()->close();

Alternatively, if you don't want to act upon changes to the reading, you can skip connecting the readingChanged signal and use the close function independently.



来源:https://stackoverflow.com/questions/29545214/detect-user-proximity-from-the-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!