Android set speakerphone on programmatically

血红的双手。 提交于 2019-11-28 10:59:16

check if your manifest file has the permissions need to do this operation.

I think this is the permission you need MODIFY_AUDIO_SETTINGS

Eran Katsav

In android 4.1 and more when you make a call the phone turn off the speaker phone automatically. So What you need to do is to add the speakerphone on in the reciever that listens for the call being made when the state is "offHook" and even put a 0.5 second delayed to turn on the speaker like that:

final Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
    @Override
    public void run() {
    audioManager.setMode(AudioManager.MODE_IN_CALL);
    audioManager.setSpeakerphoneOn(true);
    MainActivity.shouldTurnSpeakerOn = false;
    MainActivity.shouldTurnSpeakerOff = true;
    Log.d("incoming_call","speaker_on");                    
    }
}, 500);

Remember to add this to the Phone state listener. And to run off the speaker phone when the state is IDLE (needed for earlier versions).

Good Luck.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!