如何检测用户presence android系统中?用户、系统、presence、android

2023-09-06 03:26:12 作者:繁华落尽、承诺已殇!

我知道三星的Galaxy SIII,可以设置来配置一个选项,以避免屏幕关闭时,用户寻找到屏幕上。我认为手机使用照相机或一种presence传感器的。

是否有可能以编程方式做到这一点? 即使是的,有些设备就没有能力做到这一点。我想了一些可能性:使用摄像头,加速计,甚至用户活动:如果屏幕上触摸,我不知道。有一个关于用户presence到Android特定库?使用最好的传感器时,可用? 解决方案

是的,有这样的事情。

您可以使用的SensorManager 来获取传感器事件。例如,光传感器将是有用的你:

 私人的SensorManager的SensorManager;
私人传感器lightSensor;
私人浮动lightAmount;

公共无效的onCreate(包savedInstanceState){
     super.onCreate(savedInstanceState);
     的setContentView(R.layout.main);
     的SensorManager =(的SensorManager)getSystemService(SENSOR_SERVICE);
     lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);

     SensorEventListener监听器=新SensorEventListener(){
         @覆盖
         公共无效onSensorChanged(SensorEvent事件){
             //返回当前光量
             lightAmount = event.data [0];
         }

     lightSensor.registerListener(听众);
}
 

不过,当然他不能做所有的工作孤单。计划中的光线感应器,看的时候,屏幕变亮,如果是真的那的应的意味着用户不再期待它。您还可以使用加速度计(如你所说),为您排忧解难。我发现了一些code和适应它,类应该是这样的:

 公共类AccelerometerDetector {

    布尔isAvailable = FALSE;
    布尔的IsEnabled = FALSE;

    / **
     *构造函数。
     *
     *参数启用:真使加速度计
     * @throws UnsupportedOperationException异常
     *  - 抛出,如果加速度计不可用当前的设备上。
     * /
    公共AccelerometerDetector(布尔使能)
            抛出UnsupportedOperationException
    {
            / *检查传感器可用* /
            对于(字符串加速度计:Sensors.getSupportedSensors())
                    如果(accelerometer.equals(Sensors.SENSOR_ACCELEROMETER))
                            isAvailable = TRUE;

            如果(!accelerometerAvailable)
                    抛出新UnsupportedOperationException异常(
                                    加速度不可用。);

            如果(启用)
                    setEnableAccelerometer(真正的);
    }

    / **
     *如果加速度计已启用或不设置。
     *
     * @参数启用
     * @throws UnsupportedOperationException异常
     * /
    公共无效setEnableAccelerometer(布尔使能)
            抛出UnsupportedOperationException
    {
            如果(!accelerometerAvailable)
                    抛出新UnsupportedOperationException异常(
                                    加速度不可用。);

            / *如果应该启用和尚未* /
            如果(让&安培;&安培;!this.isEnabled){
                    Sensors.enableSensor(Sensors.SENSOR_ACCELEROMETER);
                    this.isEnabled = TRUE;
            }其他/ *如果要禁用,是不是已经* /
            如果(让&安培;!&安培; this.isEnabled){
                    Sensors.disableSensor(Sensors.SENSOR_ACCELEROMETER);
                    this.isEnabled = FALSE;
            }
    }

    / **
     *读取由加速度传感器提供的数值。
     *
     返回:当前的加速度值。
     * @throws UnsupportedOperationException异常
     *如果加速度不可用此设备上。
     * @throws IllegalStateException异常
     *如果加速度被禁用。
     * /
    公众持股量[] readAccelerometer()
            抛出UnsupportedOperationException,IllegalStateException异常
    {
            如果(!isAvailable)
                    抛出新UnsupportedOperationException异常(
                                    加速度不可用。);

            如果(!this.isEnabled)
                    抛出新IllegalStateException异常(
                                    加速度被禁用。);
            / *获得的传感器值的传感器将返回数量。可能
             *变量,取决于轴的量的(一维,二维或三维
             *加速度计)。 * /
            INT sensorValues​​ =传感器
                            .getNumSensorValues​​(Sensors.SENSOR_ACCELEROMETER);
            浮动[]值=新的浮动[sensorValues​​]

            / *在操作系统填补我们通过阵列。 * /
            Sensors.readSensor(Sensors.SENSOR_ACCELEROMETER,价值观);

            返回值;
    }
}
 
Android移动应用测试平台的测试方法

另外,在您的Manifest.xml声明此功能:

 <使用特征的android:NAME =android.hardware.sensor.light/>
<使用特征的android:NAME =android.hardware.sensor.accelerometer/>
 

您叫什么presence传感器可能是光/接近传感器。但你不能使用接近传感器,因为它通常只有5厘米范围。

I know in Galaxy Samsung SIII it is possible to configure in settings an option to avoid the screen turns off when user is looking into the screen. I think the phone uses the camera or a kind of a sensor of presence.

is it possible to do it programmatically? even if yes, some devices won't be capable to do that. I imagine some possibilities here: using the camera, accelerometer, or even user activity: if the screen is on, touches, I don't know. There is a specific library about "user presence" to android? Using the best of all sensors when available?

解决方案

Yes, there's something like that.

You can use SensorManager to get sensor events. For example, Light Sensor will be usefull for you:

private SensorManager sensorManager;
private Sensor lightSensor;
private float lightAmount;

public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
     lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);

     SensorEventListener listener = new SensorEventListener() {
         @Override
         public void onSensorChanged(SensorEvent event) {
             // returns the current light amount
             lightAmount = event.data[0];
         }

     lightSensor.registerListener(listener);
}

But of course he can't do all the job alone. Program your light sensor to see when the screen becomes brighter, if true that should mean the user is no longer looking to it. And you can use the accelerometer (as you said) to help you out. I found some code and adapted it, the class should be something like this:

public class AccelerometerDetector {

    boolean isAvailable = false;
    boolean isEnabled = false;

    /**
     * Constructor.
     *
     * @param enable : True to enable the accelerometer
     * @throws UnsupportedOperationException
     *  - thrown if the Accelerometer is not available on the current device.
     */
    public AccelerometerDetector(boolean enable) 
            throws UnsupportedOperationException 
    {
            /* Check if the sensor is available */
            for (String accelerometer : Sensors.getSupportedSensors())
                    if (accelerometer.equals(Sensors.SENSOR_ACCELEROMETER))
                            isAvailable = true;

            if (!accelerometerAvailable)
                    throw new UnsupportedOperationException(
                                    "Accelerometer is not available.");

            if (enable)
                    setEnableAccelerometer(true);
    }

    /**
     * Set if the Accelerometer is enabled or not.
     *
     * @param enable
     * @throws UnsupportedOperationException
     */
    public void setEnableAccelerometer(boolean enable)
            throws UnsupportedOperationException 
    {
            if (!accelerometerAvailable)
                    throw new UnsupportedOperationException(
                                    "Accelerometer is not available.");

            /* If should be enabled and isn't already */
            if (enable && !this.isEnabled) {
                    Sensors.enableSensor(Sensors.SENSOR_ACCELEROMETER);
                    this.isEnabled = true;
            } else /* If should be disabled and isn't already */
            if (!enable && this.isEnabled) {
                    Sensors.disableSensor(Sensors.SENSOR_ACCELEROMETER);
                    this.isEnabled = false;
            }
    }

    /**
     * Read the values provided by the Accelerometer.
     *
     * @return Current Accelerometer-values.
     * @throws UnsupportedOperationException
     *             if the Accelerometer is not available on this device.
     * @throws IllegalStateException
     *             if the Accelerometer was disabled.
     */
    public float[] readAccelerometer() 
            throws UnsupportedOperationException, IllegalStateException 
    {
            if (!isAvailable)
                    throw new UnsupportedOperationException(
                                    "Accelerometer is not available.");

            if (!this.isEnabled)
                    throw new IllegalStateException(
                                    "Accelerometer was disabled.");
            /* Get number of sensor-values the sensor will return. Could be
             * variable, depending of the amount of axis (1D, 2D or 3D
             * accelerometer). */
            int sensorValues = Sensors
                            .getNumSensorValues(Sensors.SENSOR_ACCELEROMETER);
            float[] values = new float[sensorValues];

            /* Make the OS fill the array we passed. */
            Sensors.readSensor(Sensors.SENSOR_ACCELEROMETER, values);

            return values;
    }
}

Also declare this feature in your Manifest.xml:

<uses-feature android:name="android.hardware.sensor.light" />
<uses-feature android:name="android.hardware.sensor.accelerometer" />

What you called "Presence Sensor" might be the Light/Proximity Sensor. But you can't use the Proximity Sensor because it usually only has 5cm range.