一、利用距离感应器监听听筒靠近耳朵事件准确的说距离感应器并不能监听到你是否把手机靠近耳朵,也许是你用手挡在了距离感应器前面,但这不是我们关心的,我们关心的是当你的耳朵靠近听筒时
一、利用距离感应器监听听筒靠近耳朵事件
准确的说距离感应器并不能监听到你是否把手机靠近耳朵,也许是你用手挡在了距离感应器前面,但这不是我们关心的,我们关心的是当你的耳朵靠近听筒时,我们要捕获到这个事件
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
}
}
--结束END--
本文标题: Android 听筒模式的具体实现实例
本文链接: https://lsjlt.com/news/27237.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0