Android set speakerphone on programmatically

旧城冷巷雨未停 提交于 2019-12-17 19:34:47

问题


I am trying to set speakerphone on in a call programmatically using audiomanager.setSpeakerphoneOn(true) in a service but it seems it is not working and I don't know why. Should I do something else?

Thanks


回答1:


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

I think this is the permission you need MODIFY_AUDIO_SETTINGS




回答2:


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.



来源:https://stackoverflow.com/questions/2663108/android-set-speakerphone-on-programmatically

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