Android通话默认打开扬声器的方法
时间:2021-01-24 11:06:25|栏目:Android代码|点击: 次
在打电话时如果想在电话接通时默认采用扬声器进行audio的输出,也就是打开免提。具体的做法是在InCallService文件中,做出下面的修改。
文件:frameworks-base / telecomm/java/android/telecom/InCallService.java
增加oncreate方法,添加phone状态监听的listener。
@Override public void onCreate() { super.onCreate(); MyPhoneStateListener phonehoneStateListener=new MyPhoneStateListener(); TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); mTelephonyManager.listen(phonehoneStateListener,PhoneStateListener.LISTEN_CALL_STATE); }
增加销毁事件
@Override public void onDestroy() { super.onDestroy(); }
增加事件的定义。
private class MyPhoneStateListener extends PhoneStateListener{ @Override public void onCallStateChanged(int state, String incomingNumber) { switch (state) { case TelephonyManager.CALL_STATE_IDLE: setAudioRoute(CallAudioState.ROUTE_EARPIECE); break; case TelephonyManager.CALL_STATE_RINGING: setAudioRoute(CallAudioState.ROUTE_SPEAKER); break; case TelephonyManager.CALL_STATE_OFFHOOK: setAudioRoute(CallAudioState.ROUTE_SPEAKER); default: break; } super.onCallStateChanged(state, incomingNumber); } }
如果是MTK平台的话,可以开启如下这个宏,也可以达到想要的效果
MTK_TB_APP_CALL_FORCE_SPEAKER_ON = yes
Android手机听筒和扬声器切换
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE); private void setSpeakerphoneOn(boolean on) { if(on) { audioManager.setSpeakerphoneOn(true); } else { audioManager.setSpeakerphoneOn(false);//关闭扬声器 audioManager.setRouting(AudioManager.MODE_NORMAL, AudioManager.ROUTE_EARPIECE, AudioManager.ROUTE_ALL); setVolumeControlStream(AudioManager.STREAM_VOICE_CALL); //把声音设定成Earpiece(听筒)出来,设定为正在通话中 audioManager.setMode(AudioManager.MODE_IN_CALL); } }