How to turn on the speaker during a call
I am trying to turn on the loudspeaker during a call. I have tried this code. Register a new listener. But it doesn't work. It shows a toast message, but the speaker is still silent (only speaker is used by default).
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.i(LOG_TAG, "onCallStateChanged: CALL_STATE_OFFHOOK");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Toast.makeText(mContext,"Call, time to turn speaker on",Toast.LENGTH_SHORT).show();
AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
audioManager.setSpeakerphoneOn(true);
break;
}
}
Of course I added permissions to my AndroidManifest.xml.
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
EDIT
This doesn't work either. It returns false (message Toast false);
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.i(LOG_TAG, "onCallStateChanged: CALL_STATE_OFFHOOK");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Toast.makeText(mContext,"Call, time to turn speaker on",Toast.LENGTH_SHORT).show();
audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
((Activity)mContext).setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setSpeakerphoneOn(true);
boolean check = audioManager.isSpeakerphoneOn();
Toast.makeText(mContext, "Speaker is on :" +String.valueOf(check),Toast.LENGTH_SHORT).show();
break;
+3
source to share
2 answers
I had the same problem due to custom rom. I installed warehouse software and everything works fine, here is my code
audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
audioManager.setMode(AudioManager.MODE_IN_CALL);
if (!audioManager.isSpeakerphoneOn())
audioManager.setSpeakerphoneOn(true);
audioManager.setMode(AudioManager.MODE_NORMAL);
+3
source to share