转自新浪博客sony9997 http://blog.sina.com.cn/s/blog_5462e93f0100w7bn.html
AudioManager
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sm.registerListener(new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
float[] its = event.values;
if (its != null&& event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
if (its[0] <= 3.0) {// 贴近手机
audioManager.setMode(AudioManager.MODE_IN_CALL);// 把模式调成听筒放音模式
} else {// 远离手机
audioManager.setMode(AudioManager.MODE_NORMAL);// 把模式调成喇叭放音模式
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
}, sm.getDefaultSensor(Sensor.TYPE_PROXIMITY),SensorManager.SENSOR_DELAY_UI);