2012-12-19 20 views
8

我知道在银河三星SIII可以在设置中配置一个选项,以避免当用户正在查看屏幕时屏幕关闭。我认为手机使用相机或存在的传感器。如何在android中检测用户的存在?

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

通常如果我想X我会寻找Y:Inactivity - > http://stackoverflow.com/questions/4208730/how-to-detect-user-inactivity-in-android还有一个答案有关用户的活动。 –

+3

SIII使用前置摄像头定期检查面部特征。 (有文档中的信息,包括通过启用它可以缩短电池寿命的警告。) –

+0

但@KenWhite,我认为还有一种“面部识别”软件(或芯片组)。你知道是否可以使用它?或者你的应用需要做你自己的脸部识别?我知道iOS有关于它的原生功能。 –

回答

5

是的,有这样的事情。

您可以使用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声明此功能:

你什么叫做“状态感应器”可能是光/接近传感器。但是你不能使用接近传感器,因为它通常只有5cm的范围。

enter image description here

+0

谢谢塞尔吉奥!我会测试它。有使用这些传感器的特殊许可? –

+1

是的!我只是忘了告诉你,我会编辑我的问题^^ –

+0

第一块代码,“光传感器”是5厘米的一个或它是“像屏幕开/关”的东西?我想如果我知道屏幕已经打开就已经够好了。 –

0

可以使用设备的正面照相机来检测用户的脸,并因此检查用户的存在。好的是,这可以在任何与前置摄像头的Android手机上工作。

Here是GitHub的图书馆,演示了如何使用Google Play Services的Vission API实现它。

相关问题