gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:53:17 25 4
gpt4 key购买 nike

我知道在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com