我的应用程序是一个拨号器,当用户拿着手机靠近他的头时,我需要关闭屏幕,防止点击控件--就像本地Android拨号的行为一样。我需要什么样的API级别,如何才能正确地做到这一点?
发布于 2014-02-19 15:01:29
下面的代码向您展示了如何使用接近传感器:
public class SensorActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mProximity;
@Override
public final void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Get an instance of the sensor service, and use that to get an instance of
// a particular sensor.
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mProximity = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
}
@Override
public final void onAccuracyChanged(Sensor sensor, int accuracy) {
// Do something here if sensor accuracy changes.
}
@Override
public final void onSensorChanged(SensorEvent event) {
float distance = event.values[0];
// Do something with this sensor data.
}
@Override
protected void onResume() {
// Register a listener for the sensor.
super.onResume();
mSensorManager.registerListener(this, mProximity, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
// Be sure to unregister the sensor when the activity pauses.
super.onPause();
mSensorManager.unregisterListener(this);
}}当脸靠近屏幕时,尝试使用接近传感器的这链接,关闭屏幕。
希望这能帮到你。
https://stackoverflow.com/questions/21884181
复制相似问题