I thought that I could turn on speakerphone and set the volume to maximum within PhoneStateListener
, but I can't get it to work when I test it. I am using AudioManager
to try to turn on the speakerphone and set the volume. Is this not the correct way to do it? Why does the following code fail to enable speakerphone?
class PlaceCall : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
...
val telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
telephonyManager.listen(CallListener(this), PhoneStateListener.LISTEN_CALL_STATE)
val callIntent = Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber))
startActivity(callIntent)
...
}
}
class CallListener(cont: Context) : PhoneStateListener() {
private var context: Context = cont
override fun onCallStateChanged(state: Int, phoneNumber: String?) {
val audioManager: AudioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL), 0) //I believe this should set call volume to max?
audioManager.isSpeakerphoneOn = true //I believe this should enable speakerphone, but it doesn't during my tests
}
}
AndroidManifest.xml
...
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
...
Edit:
I tried adding a BroadcastReceiver
as well but it didn't change the outcome. Speakerphone is still not enabled. This doesn't make sense and I don't know what I am doing wrong. Below is updated code with the new BroadcastReciever added (above code is still the same).
AndroidManifest.xml (updated)
...
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<application>
<receiver android:name=".ServiceReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
...
class ServiceReceiver : BroadcastReceiver() {
lateinit var telephonyManager: TelephonyManager
override fun onReceive(context: Context, intent: Intent) {
telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
telephonyManager.listen(CallListener(context), PhoneStateListener.LISTEN_CALL_STATE)
val audioManager: AudioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
audioManager.mode = AudioManager.MODE_IN_CALL
audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL), 0)
audioManager.isSpeakerphoneOn = true
}
}