how to turn speaker on/off programmatically in android Pie and UP
In Android Pie, I had the same problem. I resolved it using an InCallService with
setAudioRoute(ROUTE_SPEAKER)
Your app needs to be Default phone app.
I am surprised nobody mentioned TelecomManager.EXTRA_START_CALL_WITH_SPEAKERPHONE.
As an intent:
val uri = Uri.fromParts("tel", PHONE_NUMBER, null)val intentCall = Intent(Intent.ACTION_CALL, uri).apply { putExtra(TelecomManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, true) flags = Intent.FLAG_ACTIVITY_NEW_TASK}startActivity(intentCall)
Using TelecomManager.placeCall:
val uri = Uri.fromParts("tel", PHONE_NUMBER, null)val extras = Bundle().apply { putBoolean(TelecomManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, true) }val telecomManager = getSystemService(TELECOM_SERVICE) as TelecomManagertelecomManager.placeCall(uri, extras)
The default phone app should handle that extra data and enable the speakers.
Remember to ask for android.permission.CALL_PHONE
permission.
For this problem please try the following code it is working for me.
AudioManager myAudioManager;myAudioManager.setMode(AudioManager.MODE_NORMAL);myAudioManager.setSpeakerphoneOn(true);