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
}
}
上一篇: 中医养生知识 盘点冬季艾灸最常用的穴位
下一篇: 米粉糊茼蒿的做法