Fel*_*ipe 8 android user-input android-camera android-sensors
我知道在Galaxy Samsung SIII中可以在设置中配置一个选项,以避免在用户查看屏幕时屏幕关闭.我认为手机使用相机或一种存在传感器.
是的,有类似的东西.
您可以使用SensorManager获取传感器事件.例如,光传感器对您有用:
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);
}
Run Code Online (Sandbox Code Playgroud)
但他当然不能独自完成所有工作.对光传感器进行编程,以查看屏幕何时变亮,如果为真则表示用户不再需要它.你可以使用加速度计(如你所说)来帮助你.我发现了一些代码并对其进行了改编,类应该是这样的:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
还要在Manifest.xml中声明此功能:
<uses-feature android:name="android.hardware.sensor.light" />
<uses-feature android:name="android.hardware.sensor.accelerometer" />
Run Code Online (Sandbox Code Playgroud)
您称之为"存在感应器"的可能是光/接近传感器.但是你不能使用接近传感器,因为它通常只有5厘米的范围.

| 归档时间: |
|
| 查看次数: |
3920 次 |
| 最近记录: |