Android 听筒模式的具体实现实例
一、利用距离感应器监听听筒靠近耳朵事件
准确的说距离感应器并不能监听到你是否把手机靠近耳朵,也许是你用手挡在了距离感应器前面,但这不是我们关心的,我们关心的是当你的耳朵靠近听筒时,我们要捕获到这个事件
step 1,新建实现SensorEventListener接口的类并实现onSensorChanged(SensorEvent event)方法
代码如下:
public class MainActivity extends Activity implements SensorEventListener
step 2,得到距离感应器的实例
代码如下:
audioManager = (AudioManager) this
.getSystemService(Context.AUDIO_SERVICE);
mSensorManager = (SensorManager) getSystemService
(Context.SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
step 3,在onSensorChanged方法中判断是否靠近听筒
代码如下:
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
float range = event.values[0];
if (range == mSensor.getMaximumRange()) {
Toast.makeText(this, "正常模式", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "听筒模式", Toast.LENGTH_LONG).show();
}
step 4, 向SensorManager注册监听距离感应器
代码如下:
@Override
protected void onResume() {
mSensorManager.registerListener(this, mSensor,
SensorManager.SENSOR_DELAY_NORMAL);
super.onResume();
}
@Override
protected void onPause() {
mSensorManager.unregisterListener(this);
super.onPause();
}
二、根据距离感应器改变AudioManager播放模式
默认情况下,播放语音都是用外置喇叭或者耳机,我们需要编码实现播放的切换。
step 1,在AndroidManifest文件中添加权限声明
代码如下:
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
step 2,切换播放模式
代码如下:
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
float range = event.values[0];
if (range == mSensor.getMaximumRange()) {
Toast.makeText(this, "正常模式", Toast.LENGTH_LONG).show();
audioManager.setMode(AudioManager.MODE_NORMAL);
} else {
Toast.makeText(this, "听筒模式", Toast.LENGTH_LONG).show();
audioManager.setMode(AudioManager.MODE_IN_CALL);
}
完整代码:
代码如下:
package com.markjoker.sensortest;
import java.io.FileInputStream;
import java.io.IOException;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity implements SensorEventListener {
private AudioManager audioManager;
private SensorManager mSensorManager;
private Sensor mSensor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
audioManager = (AudioManager) this
.getSystemService(Context.AUDIO_SERVICE);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
MediaPlayer mPlayer = new MediaPlayer();
try {
mPlayer.reset();
mPlayer.setDataSource(new FileInputStream("/sdcard/snow.mp3").getFD());
mPlayer.prepare();
mPlayer.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void onResume() {
mSensorManager.registerListener(this, mSensor,
SensorManager.SENSOR_DELAY_NORMAL);
super.onResume();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
mSensorManager.unregisterListener(this);
super.onPause();
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
float range = event.values[0];
if (range == mSensor.getMaximumRange()) {
Toast.makeText(this, "正常模式", Toast.LENGTH_LONG).show();
audioManager.setMode(AudioManager.MODE_NORMAL);
} else {
Toast.makeText(this, "听筒模式", Toast.LENGTH_LONG).show();
audioManager.setMode(AudioManager.MODE_IN_CALL);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341