When making a phone call, if you want to use the speaker to output audio by default when the phone is connected, that is, turn on hands-free. The specific method is to make the following modifications in the InCallService file.
File: frameworks-base / telecomm/java/android/telecom/
Add the oncreate method and add the listener for phone state listening.
@Override public void onCreate() { (); MyPhoneStateListener phonehoneStateListener=new MyPhoneStateListener(); TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); (phonehoneStateListener,PhoneStateListener.LISTEN_CALL_STATE); }
Increase destruction incidents
@Override public void onDestroy() { (); }
Add the definition of the event.
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; } (state, incomingNumber); } }
If it is an MTK platform, you can enable the following macro to achieve the desired effect
MTK_TB_APP_CALL_FORCE_SPEAKER_ON = yes
Android phone handset and speaker switching
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE); private void setSpeakerphoneOn(boolean on) { if(on) { (true); } else { (false);//Switch off the speaker (AudioManager.MODE_NORMAL, AudioManager.ROUTE_EARPIECE, AudioManager.ROUTE_ALL); setVolumeControlStream(AudioManager.STREAM_VOICE_CALL); //Set the sound to Earpiece and set it to be in the call (AudioManager.MODE_IN_CALL); } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.