How To Turn Speaker On/off Programmatically In Android Pie And Up
Same as this question and many others from a few years ago: how to turn speaker on/off programmatically in android 4.0 It seems that Android has changed the way it handles this. he
Solution 1:
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.
Solution 2:
I am surprised nobody mentioned TelecomManager.EXTRA_START_CALL_WITH_SPEAKERPHONE.
As an intent:
valuri= Uri.fromParts("tel", PHONE_NUMBER, null)
valintentCall= 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 TelecomManager
telecomManager.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.
Solution 3:
For this problem please try the following code it is working for me.
AudioManager myAudioManager;
myAudioManager.setMode(AudioManager.MODE_NORMAL);
myAudioManager.setSpeakerphoneOn(true);
Post a Comment for "How To Turn Speaker On/off Programmatically In Android Pie And Up"