我知道在Galaxy Samsung SIII中,可以在设置中配置一个选项,以避免在用户查看屏幕时屏幕关闭。我认为手机使用相机或一种存在传感器。
答案 0 :(得分:6)
是的,有类似的东西。
您可以使用SensorManager来获取传感器事件。例如,Light 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厘米的范围。
答案 1 :(得分:0)
您可以使用设备的前置摄像头来检测用户的脸部,从而检查用户是否存在。好处是它可以在任何带前置摄像头的Android手机上运行。
Here是来自GitHub的图书馆,演示了如何使用Google Play服务的Vission API实现它。