android - 如何检测android中的用户存在?

标签 android android-camera user-input android-sensors

我知道在 Galaxy Samsung SIII 中,可以在设置中配置一个选项,以避免在用户注视屏幕时屏幕关闭。我认为手机使用摄像头或某种存在传感器。

  1. 是否可以通过编程方式实现?
  2. 即使是,某些设备也无法做到这一点。我在这里想象一些可能性:使用相机、加速度计,甚至用户 Activity :如果屏幕打开,触摸,我不知道。有一个关于 android 的“用户存在”的特定库?在可用时使用所有传感器中最好的?

最佳答案

是的,有类似的东西。

您可以使用 SensorManager获取传感器事件。例如,灯Sensor对你有用:

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);
}

但他当然不能独自完成所有工作。对您的光传感器进行编程以查看屏幕何时变亮,如果为真,则应该意味着用户不再注视它。你可以使用加速度计(如你所说)来帮助你。我找到了一些代码并对其进行了修改,类应该是这样的:

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;
    }
}

同时在您的 Manifest.xml 中声明此功能:

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

您所说的“存在传感器”可能是光/接近传感器。但是您不能使用接近传感器,因为它通常只有 5 厘米的范围。

enter image description here

关于android - 如何检测android中的用户存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13955782/

相关文章:

android - 当 GCMIntentService 类中的 OnUnregistered() 方法被调用时

Android:从相机保存的 Jpeg 看起来已损坏

java - 如何镜像显示OpenCV JavaCameraView前置摄像头(Android)?

scala - 使用可见的键入字符读取 Scala 中的键盘输入

c - 消除继续循环所需的 <ENTER>

java - 在 linux 上导入 gradle 后没有项目模块

android - 是否可以在 Android 中制作这样的 TableLayout?

android - 使用 CameraView 在 Android 上使用 ML Kit 检测人脸

python - 递归函数在具有错误处理的Python中要求用户输入

android - 如何更改按钮的颜色并在单击时暂停屏幕几秒钟?