我知道在 Galaxy Samsung SIII 中可以在设置中配置一个选项,以避免用户在查看屏幕时屏幕关闭。我认为手机使用摄像头或某种存在传感器。
- 是否可以通过编程来完成?
- 即使可以,某些设备也无法做到这一点。我在这里想象一些可能性:使用相机、加速计,甚至用户活动:如果屏幕打开、触摸,我不知道。 android 有一个关于“用户存在”的特定库吗?在可用的情况下使用最好的传感器?
是的,有这样的事情。
您可以使用传感器管理器 http://developer.android.com/reference/android/hardware/SensorManager.html获取传感器事件。例如,光Sensor http://developer.android.com/reference/android/hardware/Sensor.html对你有用:
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);
}
但他当然无法独自完成所有工作。对光传感器进行编程,以查看屏幕何时变亮(如果确实如此)should意味着用户不再关注它。您可以使用加速度计(如您所说)来帮助您。我找到了一些代码并对其进行了修改,该类应该是这样的:
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 厘米的范围。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)